1. 9
    Rustlings functions2: Adding a type to function arguments
    1m 38s

Rustlings functions2: Adding a type to function arguments

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet

README for this exercise.

Instructor: [0:00] In functions2, we have a function that uses call_me and passes the number three to it. Functions2 defines call_me for us and says it takes an argument of num.

[0:11] The code here uses a for loop, for i in. This is called a range. This will create a sequence from zero to whatever number we have. For example, if it was five, zero, one, two, three, and four.

[0:26] This for loop will loop over that range and use the println macro to print "Call number" and then format the number plus one into this location in the string. If we look at our error, we can see that the Rust compiler is looking for some tokens, either colon, an at, a pipe, or an ending parenthesis.

[0:46] We also see a note about anonymous parameters. If you're interested, you can look more into that, but it's not important, especially if you're learning Rust with the 2018 version like you probably are right now. We can see a number of suggestions in the help, such as if this is a self type, which we might cover later, give it a parameter name.

[1:06] If it was a parameter name, give it a type. If this is a type, explicitly ignore the parameter name by using an underscore.

[1:15] In our case, the one we want is this one. Num, for us, is a parameter name, but it doesn't have a type.

[1:22] We'll use a colon, like we did when we did variables, for this to be a u32, an unsigned 32-bit integer. We see that our range goes from zero to two, which results in the numbers one, two, and three because we add one to each in the range.