Robert Roskam
Engineer Manager at Pantheon
Organizer
@raiderrobert
twitter | github | medium
(sometimes)
fn main() {
println!("Hello World!");
}
fn main() {
let s = String::from("World!");
println!("Hello {}", s);
}
fn main() {
{
let s = String::from("World!");
}
println!("Hello {}", s);
}
rroskam$ rustc hello.rs
error[E0425]: cannot find value `s` in this scope
--> hello.rs:5:26
|
5 | println!("Hello {}", s);
| ^ not found in this scope
fn main() {
let s = String::from("World");
say_hello(s);
}
fn say_hello(some_string: String) {
println!("Hello {}!", some_string);
}
fn main() {
let s = String::from("World");
say_hello(s);
say_hello(s); // what happens here?
}
fn say_hello(some_string: String) {
println!("Hello {}!", some_string);
}
rroskam$ rustc hello.rs
error[E0382]: use of moved value: `s`
--> hello.rs:4:15
|
3 | say_hello(s);
| - value moved here
4 | say_hello(s); // compiler error
| ^ value used here after move
fn main() {
let s = String::from("World");
say_hello(s); // main moves s into say_hello
say_hello(s); // s is out of scope
}
fn say_hello(some_string: String) {
println!("Hello {}!", some_string);
}
fn main() {
let s = String::from("World");
let x = say_hello(s);
say_hello(x);
}
fn say_hello(some_string: String) -> String {
println!("Hello {}!", some_string);
return some_string
}
fn main() {
let s = String::from("World");
say_hello(s.clone());
say_hello(s.clone());
}
fn say_hello(some_string: String) {
println!("Hello {}!", some_string);
}
fn main() {
let s = String::from("World");
say_hello(&s);
say_hello(&s); // 🎉 it works!
}
fn say_hello(some_string: &String) {
println!("Hello {}!", some_string);
}
use std::env;
fn main() {
let mut args: Vec<String> = env::args().collect();
let name = args.remove(1);
say_hello(&name);
}
fn say_hello(some_string: &String) {
println!("Hello {}!", some_string);
}
use std::env;
fn main() {
let mut args: Vec<String> = env::args().collect();
let name = args.remove(1);
say_hello(&name);
say_hello(&name);
}
fn say_hello(some_string: &String) {
println!("Hello {}!", some_string);
}
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let mut name = args[1].clone();
let count:u32 = args[2].clone().parse().unwrap();
for _x in 0..count {
say_hello(&mut name);
}
}
fn say_hello(some_string: &mut String) {
some_string.push_str("🎉");
println!("Hello {} !", some_string);
}
By Robert Roskam