0
0
Rustprogramming~15 mins

Constants in Rust - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Constants in Rust
๐Ÿ“– Scenario: You are creating a simple program to calculate the area of a circle. You want to use a constant value for Pi to keep your code clean and easy to update.
๐ŸŽฏ Goal: Build a Rust program that defines a constant for Pi, uses it to calculate the area of a circle with a given radius, and prints the result.
๐Ÿ“‹ What You'll Learn
Define a constant named PI with the value 3.14159
Create a variable named radius with the value 5.0
Calculate the area using the formula PI * radius * radius
Print the area with a message
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Constants are used in programs to store fixed values like mathematical constants, configuration values, or limits that should not change during the program.
๐Ÿ’ผ Career
Understanding constants is important for writing clear, maintainable, and bug-free code in any software development job.
Progress0 / 4 steps
1
Define the constant PI
Write a constant named PI with the value 3.14159 using const and type f64.
Rust
Need a hint?

Use const keyword, specify the type f64, and assign the value 3.14159.

2
Create a variable radius
Create a variable named radius with the value 5.0 and type f64.
Rust
Need a hint?

Use let to create a variable, specify type f64, and assign 5.0.

3
Calculate the area of the circle
Create a variable named area that calculates the area using the formula PI * radius * radius.
Rust
Need a hint?

Use the formula for the area of a circle: Pi times radius squared.

4
Print the area
Print the message "The area of the circle is: " followed by the value of area using println!.
Rust
Need a hint?

Use println!("The area of the circle is: {}", area); inside fn main().