hatra1

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)
}

hatra2

fn main(){
    let mut s = String::from("hello");

    let r1 = &s;
    let r2 = &s;
    assert!(compare_strings(r1, r2));

    let r3 = &mut s;
    clear_string(r3)
}

string_from_print

fn main() {
    let s = String::from("hello");
    println!("{}", s)
}

string_from_move_print

fn main() {
    let x = String::from("hello");
    let y = x;
    println!("{}", y)
}

func_take_ownership

fn main() {
    let s = String::from("hello");
    takes_ownership(s);
    // println!("{}", s) // won't compile if added
}

fn takes_ownership(some_string: String) {
    println!("{}", some_string)
}

immutable_borrow

fn main() {
    let x = String::from("hello");
    f(&x); 
    println!("{}", x)
}

fn f(s : &String) {
    println!("{}", *s)
}

multiple_immutable_borrow

fn main() {
    let x = String::from("hello");
    let y = &x;
    let z = &x;
    f(y, z)
}

fn f(s1 : &String, s2 : &String) {
    println!("{} and {}", s1, s2)
}

mutable_borrow

fn main() {
    let mut x = String::from("Hello");
    world(&mut x);
    println!("{}", x)
}

fn world(s : &mut String) {
    s.push_str(", world")
}

nll_lexical_scope_different

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")
}

move_different_scope

fn main() {
    let x = String::from("hello");
    let z = {
        let y = x;
        println("{}", y);
        // ...
    };
    println!("Hello, world!");
}

move_assignment

fn main() {
    let x = String::from("hello");
    let mut y = String::from("test");
    y = x;
  }

move_func_return

fn f() {
    let x = String::from("hello");
    // ...
    x
} 
  
fn main() {
    let s = f();
    println!("{}", s)
}

func_take_return_ownership

fn take_and_return_ownership(some_string : String) -> String {
    println!("{}", some_string);
    some_string
}
  
fn main() {
    let mut s = String::from("hello");
    s = take_and_return_ownership(s);
    println!("{}", s);   // OK
}

immutable_borrow_method_call

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)
}

mutable_borrow_method_call

fn main() { 
    let mut s1 = String::from("Hello");
    let s2 = String::from(", world");
    String::push_str(&mut s1, &s2); 
    s1.push_str(&s2); // shorthand for the above
    println!("{}", s1); // prints "Hello, world, world"
}

immutable_variable

fn main() {
    let x = 5;
}

mutable_variables

fn main() {
    let mut x = 5;
    x = 6; //OK
}

copy

fn main() {
    let x = 5;
    let y = x;
}

function

fn main() {
    let six = plus_one(5);
}

fn plus_one(x: i32) -> i32 {
    x + 1
}

printing

fn main() {
    let x = 1;
    let y = 2;
    println!("x = {} and y = {}", x, y);
}

struct_string

struct Foo {
    x: i32,
    y: String,
}

fn main() {
    let _y = String :: from("bar");
    let f = Foo { x: 5, y: _y };
    println!("{}", f.x);
    println!("{}", f.y);
}

struct_rect

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_rect2

struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn print_area(rect: &Rectangle) {
    println!(
        "The area of the rectangle is {} square pixels.",
       	rect.area() // dot even though it's actually a reference
    );
}

fn main() {
    let r = Rectangle {
        width: 30,
        height: 50,
    };
   	print_area(&r);
}

struct_lifetime

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,
    };
}