Skip to content

Commit

Permalink
new challenge variable declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 6, 2024
1 parent 4c6e49d commit f4361ee
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions challenges/challenges.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions challenges/declaring-variables/Cargo.toml
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" }
37 changes: 37 additions & 0 deletions challenges/declaring-variables/description.md
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.
11 changes: 11 additions & 0 deletions challenges/declaring-variables/src/lib.rs
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
}
6 changes: 6 additions & 0 deletions challenges/declaring-variables/src/starter.rs
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
}
12 changes: 12 additions & 0 deletions challenges/declaring-variables/tests/tests.rs
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");
}

0 comments on commit f4361ee

Please sign in to comment.