O'Reilly logo

Mastering Rust by Vesa Kaihlavirta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Function parameters and patterns

The same system of moves and copies works for other variable bindings besides just the let form. If you pass parameters to functions, the same rules are in effect:

// functions.rs fn take_the_n(n: u8) { } fn take_the_s(s: String) { } fn main() {     let n = 5;     let s = String::from("string");     take_the_n(n);     take_the_s(s);     println!("n is {}", n);     println!("s is {}", s); } 

The compilation fails in a familiar way:

The String type does not implement the Copy trait, so the ownership of the value is moved to the take_the_s function. When that function's block ends, the scope of the value is finished and it is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required