Rust Programming Tutorial #13 - Functions

preview_player
Показать описание
In this video I go over how you can write and use your own functions using Rust - we look at passing in values and also functions that return data.

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
Рекомендации по теме
Комментарии
Автор

I split my screen in one part this video and the other part the interactive Rust playground, so I'm coding along with the video. And that works great for me.
So I just wanna give a shout out to the person who made these videos: Great for learning and helps me tremendously. Thanks dude!

drizerreal
Автор

I knew Rust can return values, but I've never seen people use the return keyword.

They always just do something like this:

fn is_even(n: u32) -> book { n % 2 == 0 }

Leaving off the return keyword as well as the semicolon for the final and returned expression.

kiraPhk
Автор

I dont know the 2017 version how was it.

But now you can omit the return, and the last line achivable will be the return statment

faller
Автор

I love your rust videos, why did you stop doin rust?

arcticspacefox
Автор

just to add, in case it's not covered elsewhere in your excellent videos, can also do stuff like this:
fn main() {
print_numbers_to(20);
}

fn print_numbers_to(limit:u32){
for n in 1..=limit{
println!("{} is {}", n, if n%2==0{"even"}else{"odd"});
}
}

samdavepollard
Автор

The functions are sorta like Go. You hove to specify the type of data you are passing.

the_real_editor
Автор

"for n in 1..num" will print 1 to 9, if you want 1 to 10 "for n in 1..=num" (or of course 1..num+1 )

NouifrUIwefdf
Автор

how to use magic_number() function to generate unsigned values (0 or 1)

Footballistaas
Автор

Hi, I'm on learning rust right now and watching this video to find some practical examples.
I thought println! on line, 8 will error because the ownership is transferred to is_even function on line 7


But it didn't :|

adiyatmubarak
Автор

it shall work without "return", it is not necessary element

Romancrabf
Автор

Actually, if a function has the name print_number_to, then the last number has to be included, and thus it must be "for n in 1..=num"

oaw_manofnorse