]> git.proxmox.com Git - rustc.git/blob - src/doc/book/listings/ch04-understanding-ownership/listing-04-03/src/main.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / doc / book / listings / ch04-understanding-ownership / listing-04-03 / src / main.rs
1 fn main() {
2 let s = String::from("hello"); // s comes into scope
3
4 takes_ownership(s); // s's value moves into the function...
5 // ... and so is no longer valid here
6
7 let x = 5; // x comes into scope
8
9 makes_copy(x); // x would move into the function,
10 // but i32 is Copy, so it's okay to still
11 // use x afterward
12
13 } // Here, x goes out of scope, then s. But because s's value was moved, nothing
14 // special happens.
15
16 fn takes_ownership(some_string: String) { // some_string comes into scope
17 println!("{}", some_string);
18 } // Here, some_string goes out of scope and `drop` is called. The backing
19 // memory is freed.
20
21 fn makes_copy(some_integer: i32) { // some_integer comes into scope
22 println!("{}", some_integer);
23 } // Here, some_integer goes out of scope. Nothing special happens.