1. 42
    Rustlings move_semantics4: Initializing a vector with the vec! macro
    57s

Rustlings move_semantics4: Initializing a vector with the vec! macro

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

README for this exercise.

Chris Biscardi: [0:00] Move_semantics4 set up in a very similar way to the last three move_semantics exercises. The difference is that fill_vec() doesn't take anything as an argument.

[0:09] On line 23, the Rust compiler tells us that it expected value and found macro 'vec'. That's because to initialize a new vector, we can use the vec! Macro.

[0:17] Note that there are a number of values we can use here where we're going to use an empty vector to start. Also, note that the Rust compiler expected us to do something after the vec! Macro.

[0:26] Finally, we're still passing in vec0 on line 12 to the fill_vec() function. Fill_vec() doesn't take any arguments, so we will remove zero. Note that Vec: :new can no longer infer of the type that the values inside of the vector in half, but this is fine because we aren't actually using vec0 anymore.

[0:44] We'll just delete that entire line. Now our code compiles. We have a mutable vector that we fill via a separate function that takes no arguments and returns us a new vector created with the vec! Macro that we can then push(88) into later.