-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "declaring-variables" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[dev-dependencies] | ||
syntest = { path = "../../crates/syntest" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
In Rust, variables are immutable by default. This means that once a value is bound to a variable, it cannot be changed. This is a key feature of Rust that helps ensure safety and prevent bugs by avoiding unexpected changes to values. | ||
|
||
In this challenge, you will declare and use immutable variables in Rust. You will be given a function where you need to declare variables and use them to perform specific operations. The goal is to get comfortable with the concept of immutability and understand how to use immutable variables effectively. | ||
|
||
## Your task | ||
|
||
Your task is to complete the given function by declaring immutable variables and using them to perform specific operations. You need to calculate the area of a rectangle and return the result. | ||
|
||
## Requirements | ||
|
||
- Declare two immutable variables, `width` and `height`, and assign them values. | ||
- Calculate the area of the rectangle using the formula `area = width * height`. | ||
- Return the calculated area. | ||
|
||
## Example | ||
|
||
```rust | ||
fn calculate_area() -> u32 { | ||
// Declare width and height | ||
let width = 10; | ||
let height = 5; | ||
|
||
// Calculate area | ||
let area = width * height; | ||
|
||
// Return area | ||
area | ||
} | ||
|
||
assert_eq!(calculate_area(), 50); | ||
``` | ||
|
||
## Hints | ||
|
||
- Use the `let` keyword to declare variables. | ||
- Remember that variables declared with `let` are immutable by default. | ||
- Use multiplication `*` to calculate the area. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub fn calculate_area() -> u32 { | ||
// Declare width and height as immutable variables | ||
let width = 10; | ||
let height = 5; | ||
|
||
// Calculate the area of the rectangle | ||
let area = width * height; | ||
|
||
// Return the calculated area | ||
area | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub fn calculate_area() -> u32 { | ||
// TODO: Implement the function here | ||
// Declare width and height as immutable variables | ||
// Calculate the area of the rectangle | ||
// Return the calculated area | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use declaring_variables::*; | ||
use syntest::Syntest; | ||
|
||
#[test] | ||
fn test_calculate_area() { | ||
assert_eq!(calculate_area(), 50); | ||
} | ||
|
||
#[test] | ||
fn test_variable_width() { | ||
let syntest = Syntest::from("./src/lib.rs"); | ||
} |