Functions and Control Flow in Rust

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

fn identity<T>(value: T) -> T {
    value
}

fn main() {
    let v = plus_one(5);
    let x = identity(42);
    let y = identity("Hello");
    let z = identity(3.14);
    
    println!("v = {}", v); 
    println!("x = {}", x);
    println!("y = {}", y);
    println!("z = {}", z);
}
fn main() {
    test_if_else(-1);

    test_if_else(50);
}

fn test_if_else(n: i32) {
    if n < 0 {
        print!("{} is negative", n);
    } else if n > 0 {
        print!("{} is positive", n);
    } else {
        print!("{} is zero", n);
    }

    let big_n =
    if n < 10 && n > -10 {
        println!(", and is a small number, increase ten-fold");

        // This expression returns an `i32`.
        10 * n
    } else {
        println!(", and is a big number, halve the number");

        // This expression must return an `i32` as well.
        n / 2
        // TODO ^ Try suppressing this expression with a semicolon.
    };
    //   ^ Don't forget to put a semicolon here! All `let` bindings need it.
    println!("{} -> {}", n, big_n);
}
fn test_loop(mut count: u32) {
    println!("Let's count until infinity!");
    'outer: loop {
        loop {
            count += 1;
            if count == 3 {
                println!("three");
                continue;
            }
            println!("{}", count);
            if count == 5 {
                println!("OK, that's enough.Exited the inner loop");
                break; // This would break only the inner loop
            }
            if count > 5 {
                println!("Exited the outer loop");
                break 'outer; // This breaks the outer loop
            }
        }
    }
    println!("Exited test_loop!");
}

fn test_while(mut n: i32) {
    // Loop while `n` is less than 6
    while n < 6 {
        if n % 15 == 0 {
            println!("fizzbuzz");
        } else if n % 3 == 0 {
            println!("fizz");
        } else if n % 5 == 0 {
            println!("buzz");
        } else {
            println!("{}", n);
        }
        // Increment counter
        n += 1;
    }
}

fn main() {
    test_loop(1);
    test_while(1);
}
fn test_for_range() {
    // `n` will take the values:
    //     1, 2, ..., 5 in each iteration
    for n in 1..6 {
        if n % 15 == 0 {
            println!("fizzbuzz");
        } else if n % 3 == 0 {
            println!("fizz");
        } else if n % 5 == 0 {
            println!("buzz");
        } else {
            println!("{}", n);
        }
    }
}

fn test_for_iterators(names: Vec<&str>) {
    for name in names.into_iter() {
        match name {
            "Ferris" => {
                println!("There is a rustacean among us!")
            },
            _ => println!("Hello {}", name),
        }
    }
}

fn main() {
    let names = vec!["Bob", "Frank", "Ferris"];
    test_for_iterators(names);

    test_for_range();
}