Last active
March 17, 2020 15:55
-
-
Save NebulaFox/476ba895333b1efa36892d630e68e230 to your computer and use it in GitHub Desktop.
move semantic
This file contains hidden or 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
use rand; // 0.7.3 | |
use rand::Rng; | |
fn make_a_string_with(string: &str) -> String { | |
let mut s = String::new(); | |
s.push_str(string); | |
let mut rng = rand::thread_rng(); | |
let r = rng.gen_range(0, 100); | |
for _ in 0..r { | |
s.push_str("!"); | |
} | |
s | |
} | |
fn make_a_string_move(string: String) -> String { | |
let mut s = string; | |
let mut rng = rand::thread_rng(); | |
let r = rng.gen_range(0, 100); | |
for _ in 0..r { | |
s.push_str("!"); | |
} | |
s | |
} | |
fn main() { | |
// how long is a piece of string | |
let s = make_a_string_with("Hello World"); | |
println!("len: {}, string: {}", s.len(), s); | |
let moved_string = String::from("PANIC"); | |
let m = make_a_string_move(moved_string); // move | |
println!("len: {}, string: {}", m.len(), m); | |
// println!("len: {}, string: {}", moved_string.len(), moved_string); | |
// ^^^ Compiler error if uncommented | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment