1/5
# If and else statement ## Example Execute the following command to run [`./solutions/examples/if_else.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/if_else/solutions/examples/if_else.rs) ```shell cargo run --example if_else ``` ## Exercises Exercises are in [`./exercises/src/lib.rs`](https://github.com/Cyfrin/rust-crash-course/blob/main/topics/if_else/exercises/src/lib.rs) ### Exercise 1 ```rust pub fn min(x: i32, y: i32) -> i32 { todo!(); } ``` Return the minimum of `x` and `y`. ### Exercise 2 ```rust pub fn max(x: i32, y: i32) -> i32 { todo!(); } ``` Return the maximum of `x` and `y`. ### Exercise 3 ```rust pub fn sign(x: i32) -> i32 { todo!(); } ``` Return the sign of `x`. If `x` is negative, return -1, otherwise return 1. ## Test ```shell cargo test ```
Execute the following command to run ./solutions/examples/if_else.rs
Exercises are in ./exercises/src/lib.rs
Return the minimum of x
and y
.
Return the maximum of x
and y
.
Return the sign of x
.
If x
is negative, return -1, otherwise return 1.
A hands-on introduction to If and else statement - Learn to make decisions and control execution paths in Rust using `if` and `else` statements. Solidify your understanding by implementing functions to find the minimum, maximum, and sign of numbers.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025
Rust Developer
Rust Programming BasicsDuration: 6min
Duration: 18min
Duration: 47min
Duration: 15min
Duration: 19min
Duration: 8min
Duration: 12min
Duration: 46min
Duration: 14min
Course Overview
About the course
Introduction to the Rust programming language
Rust variables and functions
Scalar types, arrays, strings, enum, structs, vectors, and hash maps in Rust
Rust control flows: If / else, if let and let else, loop, match
Rust ownership, including borrow and references
Rust error handling
Rust Modules
Rust Traits
Last updated on July 11, 2025