forked from inancgumus/learnrust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
85 lines (74 loc) · 2.62 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// WHAT IS A REFERENCE?
//
// It's a value that refers to another value
// without taking its ownership.
//
// Represented with a leading ampersand &.
//
// &s let s = String::from("hi") "hi"
//
// name | value name | value index | value
// ---- + ------ ---- + ------ ----- + -----
// ptr | 0x5 -------> ptr | 0x01 -------> 0 | h
// len | 2 1 | i
// cap | 2 ----- + -----
// -----+-------
//
fn main() {
let s = String::from("hi");
let l = strlen(&s); // strlen BORROWS s.
// strlen doesn't own it.
// main is the OWNER.
// strlen is the BORROWER.
println!("len({}) = {}", s, l); // that's why we can still use s here.
//
let mut cs = s; // cs is the owner of s now.
change(&mut cs); // send a mutable ref of cs with change()
// -> so change() can change it.
println!("{}", cs); // we can still use cs because
// we're the owner of it.
println!("{:?}", cs);
// ========================================================================
// MULTIPLE MUTABLE REFERENCES
//
// let mut s = String::from("hey");
//
// IN THE SAME SCOPE:
//
// -> There can be only a single mutable borrower (reference).
// {
// let mutRefToS = &mut s;
//
// }
// mutRefToS goes out of scope, Rust drops it.
// That's why you can make a new reference to it here.
//
// let mutRef2ToS = &mut s;
//
// -> There can be multiple non-mutable borrowers.
// let rs1 = &s; // immutable borrower
// let rs2 = &s; // immutable borrower
//
// -> There can't be a mutable borrower if there are immutable borrowers.
// let rs3 = &mut s; // mutable borrower
// println!("{} {} {}", rs1, rs2, rs3);
//
// ========================================================================
} // the main is the owner of s, and cs.
// they go out of scope and but Rust drops them.
fn strlen(s: &String) -> usize { // s is a reference to a String
s.len()
} // s goes out of scope but nothing happens.
// because strlen isn't the owner of s,
// the main() is.
/*
this won't work.
s is not a mutable reference.
fn change(s: &String) {
s.push_str(" there!");
}
*/
fn change(s: &mut String) {
s.push_str(" there!");
}