Ownership in action

Apart from the let binding example, there are other places where you will find ownership in effect, and it's important to recognize these and the errors the compiler gives us.

Functions: If you pass parameters to functions, the same ownership rules are in effect:

// ownership_functions.rsfn 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 similar way:

String does not implement the Copy trait, so the ownership of the value is moved inside the take_the_s

Get The Complete Rust Programming Reference Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.