summaryrefslogtreecommitdiff
path: root/rust/chap3/flow.rs
blob: 36557e554f4e29ff46d3988d03d224b7e4c7adc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

fn if_me() {
    let number = 6;

    if number < 5 {
        println!("{number}");
    } else {
        println!("Number too high");
    }

    let choice = false;

    let b = if choice { 24 } else { 64 };

    println!("Choice is {b}");
}

fn loop_me() {

    let mut c = 5;

    /*
     * Rust allow loops to me labeled, and use such labels into
     * break and continue commands to decide what loop to break/continue
     */
    let res = 'loop_label: loop {
        println!("Again");

        if c == 0 {
            break 'loop_label 99;   // Return a value from the loop to the variable
        }

        c = c - 1;
    };

    println!("Value returned by the loop {res}");
}

fn for_me() {
    unimplemented!();
}

fn while_me() {
    unimplemented!();
}

fn main() {
    if_me();
    loop_me();
}