-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrref.rs
35 lines (32 loc) · 1.05 KB
/
strref.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
fn main() {
let s = "hello".to_string();
let rs = &s;
let rrs: &&String = &rs;
// Prints hello.
println!("{}", *rrs);
let s = "hello".to_string();
// This takes s by reference.
// Clippy will warn you that you don't need to clone here.
let cs: String = s.clone();
// Prints hello.
println!("{}", cs);
let s = "hello".to_string();
let rs = &s;
// This takes rs by reference, but then dereferences it automatically.
let cs: String = rs.clone();
// Prints hello.
println!("{}", cs);
let s: &str = "hello";
// This would clone the &str reference, so it fails to compile.
// let cs: String = s.clone();
let cs: String = s.to_owned();
// Also acceptable above would have been:
// let cs: String = s.to_string();
// let cs: String = String::from(s);
// let cs: String = s.into();
// Those last two will eat a copy of s, since &str is copy, since
// references are copy. So you could still do this:
// println!("{}", s);
// Prints hello.
println!("{}", cs);
}