diff --git a/Cargo.lock b/Cargo.lock index 986fdf0..42e8e84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -247,6 +247,13 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" name = "countdown" version = "0.1.0" +[[package]] +name = "declaring-variables" +version = "0.1.0" +dependencies = [ + "syntest", +] + [[package]] name = "determine-number-characteristics" version = "0.1.0" diff --git a/challenges/challenges.json b/challenges/challenges.json index 04b9f04..2f91b85 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -11,6 +11,18 @@ "created_at": "2024-04-20T00:00:00Z", "updated_at": "2024-04-20T00:00:00Z" }, + { + "id": 22, + "title": "Declaring Variables", + "slug": "declaring-variables", + "short_description": "Learn to declare immutable variables in Rust and understand their usage.", + "language": "RUST", + "difficulty": "BEGINNER", + "track": "RUST_BASICS", + "tags": ["variables", "immutability"], + "created_at": "2024-06-06T00:00:00Z", + "updated_at": "2024-06-06T00:00:00Z" + }, { "id": 2, "title": "Character counting string", diff --git a/challenges/declaring-variables/Cargo.toml b/challenges/declaring-variables/Cargo.toml new file mode 100644 index 0000000..5751d5f --- /dev/null +++ b/challenges/declaring-variables/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "declaring-variables" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[dev-dependencies] +syntest = { path = "../../crates/syntest" } diff --git a/challenges/declaring-variables/description.md b/challenges/declaring-variables/description.md new file mode 100644 index 0000000..b0e3826 --- /dev/null +++ b/challenges/declaring-variables/description.md @@ -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. diff --git a/challenges/declaring-variables/src/lib.rs b/challenges/declaring-variables/src/lib.rs new file mode 100644 index 0000000..4e13e25 --- /dev/null +++ b/challenges/declaring-variables/src/lib.rs @@ -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 +} diff --git a/challenges/declaring-variables/src/starter.rs b/challenges/declaring-variables/src/starter.rs new file mode 100644 index 0000000..ab8dd5a --- /dev/null +++ b/challenges/declaring-variables/src/starter.rs @@ -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 +} diff --git a/challenges/declaring-variables/tests/tests.rs b/challenges/declaring-variables/tests/tests.rs new file mode 100644 index 0000000..36d74ba --- /dev/null +++ b/challenges/declaring-variables/tests/tests.rs @@ -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"); +}