fn main() {
let s = String::from("hello");
takes_ownership(s);
let mut x = 5;
let y = x;
x = 6
}
fn takes_ownership(some_string: String) {
println!("{}", some_string)
}
fn main() {
let mut x = String::from("Hello");
let y = &mut x;
world(y);
let z = &mut x; // OK, because y's lifetime has ended (last use was on previous line)
world(z);
x.push_str("!!"); // Also OK, because y and z's lifetimes have ended
println!("{}", x)
}
fn world(s : &mut String) {
s.push_str(", world")
}
fn main() {
let s = String::from("hello");
let len1 = String::len(&s);
let len2 = s.len(); // shorthand for the above
println!("len1 = {} = len2 = {}", len1, len2)
}
struct Rect {
w: u32,
h: u32,
}
fn main() {
let r = Rect {
w: 30,
h: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
area(&r)
);
println!(
"The height of that is {}.", r.h
);
}
fn area(rect: &Rect) -> u32 {
rect.w * rect.h
}
struct Excerpt<'a> {
p: &'a str,
}
fn main() {
let n = String::from("Ok. I'm fine.");
let first = n.split('.').next().expect("Could not find a '.'");
let i = Excerpt {
p: first,
};
}