A Rust programming language example to teach immutable borrowing: a stainless steel tank filling problem
Let's write a simple Rust program to illustrate the concept of immutable borrowing. We will model a stainless steel tank with the dimensions of height, length, and width. The tank will hold purified water, which will be filled using a pump.
The tank's capacity will be calculated in cubic meters. To start, the program will randomly generate the height, length, and width of the tank. These values should be borrowed immutably and used to calculate the tank’s capacity.
Next, we will simulate the filling process. The pumping capacity (in cubic meters per hour) will also be randomly generated each time the program runs. Assuming the tank starts empty, the program will calculate how long (in hours) it will take to completely fill the tank.
This exercise will help students understand the concept of immutable borrowing in Rust.
Here’s a simple Rust program that demonstrates the concept of immutable borrowing while modeling the stainless steel tank and simulating the filling process:
use rand::Rng;
// Function to calculate the tank capacity
fn calculate_capacity(height: &f64, length: &f64, width: &f64) -> f64 {
height * length * width
}
// Function to calculate time to fill the tank
fn time_to_fill_tank(capacity: &f64, pump_rate: &f64) -> f64 {
capacity / pump_rate
}
fn main() {
// Randomly generate dimensions of the tank (height, length, width)
let mut rng = rand::thread_rng();
let height: f64 = rng.gen_range(1.0..10.0); // Height in meters
let length: f64 = rng.gen_range(1.0..10.0); // Length in meters
let width: f64 = rng.gen_range(1.0..10.0); // Width in meters
// Borrow the dimensions immutably to calculate the tank's capacity
let capacity = calculate_capacity(&height, &length, &width);
println!("Tank dimensions: height = {:.2}m, length = {:.2}m, width = {:.2}m", height, length, width);
println!("Tank capacity: {:.2} cubic meters", capacity);
// Randomly generate the pump rate (cubic meters per hour)
let pump_rate: f64 = rng.gen_range(1.0..5.0);
println!("Pump rate: {:.2} cubic meters per hour", pump_rate);
// Calculate the time (in hours) to fill the tank
let time = time_to_fill_tank(&capacity, &pump_rate);
println!("Time to fill the tank: {:.2} hours", time);
}
Key Points:
- Immutable Borrowing: The dimensions (
height
,length
, andwidth
) and thecapacity
are borrowed immutably by the functionscalculate_capacity
andtime_to_fill_tank
. This ensures that these values cannot be modified within the functions. - Random Value Generation: The dimensions of the tank and the pump rate are randomly generated using the
rand
crate. - Calculation of Capacity: The tank’s capacity is calculated by multiplying its height, length, and width.
- Time Calculation: The time to fill the tank is calculated by dividing the tank's capacity by the pump rate.
Running the Code:
To run this code, ensure you have the rand
crate added to your Cargo.toml
:
[dependencies]
rand = "0.8"
This simple program helps illustrate the concept of immutable borrowing while performing a useful calculation.