Skip to content

Instantly share code, notes, and snippets.

View ihcsim's full-sized avatar

Ivan Sim ihcsim

View GitHub Profile
type Service struct {
name string
namespace string
}
func main() {
svc1 := Service{
name: "nginx",
namespace: "default",
}
fn main() {
let mut signal = "kill".to_string();
loop {
exit(signal);
signal = "kill".to_string();
}
}
$ cargo run
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership)
error[E0382]: use of moved value: `signal`
--> src/main.rs:4:14
|
2 | let signal = "kill".to_string();
| ------ move occurs because `signal` has type `String`, which does not implement the `Copy` trait
3 | loop {
4 | exit(signal)
| ^^^^^^ value moved here, in previous iteration of loop
fn main() {
let signal = "kill".to_string();
loop {
exit(signal);
}
}
fn exit(s: String) {
if s == "kill" {
println!("exiting with signal {}", s);
$ cargo run
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership)
error[E0382]: borrow of moved value: `s`
--> src/main.rs:26:9
|
25 | let s = "some data".to_string();
| - move occurs because `s` has type `String`, which does not implement the `Copy` trait
26 | (s, s.len())
| - ^^^^^^^ value borrowed here after move
| |
func main() {
b, n := read()
fmt.Printf("data: %s\nlen: %d\n", b, n)
}
func read() (string, int) {
s := "some data"
return s, len(s)
}
$ cargo run -q
svc: nginx.defaultsvc.cluster.local
fn main() {
let svc = "nginx.default".to_string();
let strict = true;
if strict {
println!("svc: {}", fqdn(svc));
} else {
println!("svc: {}", svc);
}
println!("svc: {}", svc);
}
func main() {
svc := "nginx.default"
strict := true
if strict {
fmt.Printf("%s\n", fqdn(svc))
} else {
fmt.Printf("%s\n", svc)
}
}
$ cargo run
Compiling rust-ownership v0.1.0 (/home/isim/rust-ownership)
error[E0505]: cannot move out of `str` because it is borrowed
--> src/main.rs:3:62
|
3 | println!("original: {}\nupdated: {}", str, append_suffix(str));
| ---------------------------------------------------------^^^--
| | | |
| | | move out of `str` occurs here
| | borrow of `str` occurs here