Follow these steps:
- Create a file named constant.rs with the next code snippet.
- Declare the global UPPERLIMIT using constant:
// Global variables are declared outside scopes of other
function
const UPPERLIMIT: i32 = 12;
- Create the is_big function by accepting a single integer as input:
// function to check if bunber
fn is_big(n: i32) -> bool {
// Access constant in some function
n > UPPERLIMIT
}
- In the main function, call the is_big function and perform the decision-making statement:
fn main() {
let random_number = 15;
// Access constant in the main thread
println!("The threshold is {}", UPPERLIMIT);
println!("{} is {}", random_number, if
is_big(random_number) { "big" } else { "small"
});
// Error! Cannot modify a `const`.
// UPPERLIMIT = 5;
}
You should get the following screenshot as output upon running the preceding code: