This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let (b, n) = read(); | |
println!("{} {}", b, n); | |
} | |
fn read() -> (String, usize) { | |
let s = "some data".to_string(); | |
(s, s.len()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cargo run | |
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership) | |
error[E0382]: borrow of moved value: `svc` | |
--> src/main.rs:9:25 | |
| | |
2 | let svc = "nginx.default".to_string(); | |
| --- move occurs because `svc` has type `String`, which does not implement the `Copy` trait | |
... | |
5 | println!("svc: {}", fqdn(svc)); | |
| --- value moved here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let svc = "nginx.default".to_string(); | |
let strict = true; | |
if strict { | |
println!("svc: {}", fqdn(svc)); | |
} else { | |
println!("svc: {}", svc); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let str = "hello".to_string(); | |
println!("original: {}\nupdated: {}", str, append_suffix(str)); | |
} | |
fn append_suffix(s: String) -> String { | |
s + " world" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let str1 = "Hello world".to_string(); | |
let str2 = str1; | |
println!("{}\n{}", str1, str2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cargo run | |
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership) | |
error[E0382]: borrow of moved value: `svcs` | |
--> src/main.rs:22:22 | |
| | |
8 | let svcs = vec![ | |
| ---- move occurs because `svcs` has type `Vec<Service>`, which does not implement the `Copy` trait | |
... | |
19 | for svc in svcs { | |
| ---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Service struct { | |
name string | |
namespace string | |
} | |
func main() { | |
svcs := []Service{ | |
{ | |
name: "nginx", | |
namespace: "default", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cargo run | |
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership) | |
error[E0507]: cannot move out of index of `Vec<Service>` | |
--> src/main.rs:19:17 | |
| | |
19 | let nginx = svcs[0]; | |
| ^^^^^^^ | |
| | | |
| move occurs because value has type `Service`, which does not implement the `Copy` trait | |
| help: consider borrowing here: `&svcs[0]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Service struct { | |
name string | |
namespace string | |
} | |
func main() { | |
svcs := []Service{ | |
{ | |
name: "nginx", | |
namespace: "default", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cargo run | |
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership) | |
error[E0382]: borrow of partially moved value: `svc1` | |
--> src/main.rs:18:28 | |
| | |
15 | namespace: svc1.namespace, | |
| -------------- value partially moved here | |
... | |
18 | println!("{:?}\n{:?}", svc1, svc2); | |
| ^^^^ value borrowed here after partial move |