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.


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/" */
pub fn bigger(a: i32, b: i32) -> i32 {
    if a > b {
    } else {

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`.
mod tests {
    use super::*; // To use all variables & functions from the parent module.

    // Any test function must be decorated with `#[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/" */
pub fn fizz_if_foo(fizzish: &str) -> &str {
    if fizzish == "fizz" {
    } else if fizzish == "fuzz" {
    } else {

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

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

Continue with Rustlings Solution