Rustlings Topic: If

Have a look at Control Flow - if expressions to check further information about Rust functions.

You may find solution code for the topic from my repo.

  1. if1.rs
  2. if2.rs

if1.rs

Remember in Rust that:

  • the if condition does not need to be surrounded by parentheses
  • if/else conditionals are expressions
  • Each condition is followed by a {} block.

Notice that there is no ; after a or b. Since both will be returned from the function.

/* file: "exercises/if/if1.rs" */
pub fn bigger(a: i32, b: i32) -> i32 {
    if a > b {
        a
    } else {
        b
    }
}

if2.rs

First, Rust requires each conditional block to return the same value. So 1 is not acceptable and won’t compile.

In order to change it to the proper value, we have to check how the test is configured.

And if you have not noticed the basic testing format rust provides yet.
The structure looks like this:

// #[cfg(test)] macro indicates that below tests module is indeed the `test`.
#[cfg(test)]
mod tests {
    use super::*; // To use all variables & functions from the parent module.

    // Any test function must be decorated with `#[test]`.
    #[test]
    fn test_name() {

    }
}

Back to the problem, we have to return bar if the input value is fuzz and baz for anything else.

/* file: "exercises/if/if1.rs" */
pub fn fizz_if_foo(fizzish: &str) -> &str {
    if fizzish == "fizz" {
        "foo"
    } else if fizzish == "fuzz" {
        "bar"
    } else {
        "baz"
    }
}

You can also use match control flow operation to do the same.

/* file: "exercises/if/if1.rs" */
pub fn fizz_if_foo(fizzish: &str) -> &str {
    match fizzish {
        "fizz" => "foo",
        "fuzz" => "bar",
        _ => "baz",
    }
}

Continue with Rustlings Solution