Skip to content

Instantly share code, notes, and snippets.

View ihcsim's full-sized avatar

Ivan Sim ihcsim

View GitHub Profile
fn main() {
let (b, n) = read();
println!("{} {}", b, n);
}
fn read() -> (String, usize) {
let s = "some data".to_string();
(s, s.len())
}
$ 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
fn main() {
let svc = "nginx.default".to_string();
let strict = true;
if strict {
println!("svc: {}", fqdn(svc));
} else {
println!("svc: {}", svc);
}
}
fn main() {
let str = "hello".to_string();
println!("original: {}\nupdated: {}", str, append_suffix(str));
}
fn append_suffix(s: String) -> String {
s + " world"
}
fn main() {
let str1 = "Hello world".to_string();
let str2 = str1;
println!("{}\n{}", str1, str2);
}
$ 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 {
| ----
type Service struct {
name string
namespace string
}
func main() {
svcs := []Service{
{
name: "nginx",
namespace: "default",
$ 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]`
type Service struct {
name string
namespace string
}
func main() {
svcs := []Service{
{
name: "nginx",
namespace: "default",
$ 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