Skip to content

Commit

Permalink
mutable vars
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 8, 2024
1 parent 11daae8 commit 645c41f
Show file tree
Hide file tree
Showing 7 changed files with 132 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 @@ -23,6 +23,18 @@
"created_at": "2024-06-07T00:00:00Z",
"updated_at": "2024-06-07T00:00:00Z"
},
{
"id": 23,
"title": "Mutable Variables",
"slug": "mutable-variables",
"short_description": "Declare and manipulate mutable variables in Rust.",
"language": "RUST",
"difficulty": "EASY",
"track": "RUST_BASICS",
"tags": [],
"created_at": "2024-06-08T00:00:00Z",
"updated_at": "2024-06-08T00:00:00Z"
},
{
"id": 2,
"title": "Character counting string",
Expand Down
7 changes: 7 additions & 0 deletions challenges/mutable-variables/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "mutable-variables"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
syntest = { path = "../../crates/syntest" }
41 changes: 41 additions & 0 deletions challenges/mutable-variables/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
## Mutable Variables Challenge

Declaring and manipulating variables in programming is a fundamental concept that allows you to store and modify data. Variables in Rust are immutable by default, but you can make them mutable using the `mut` keyword.

In this challenge, you will declare and use mutable variables in Rust. You will be given a function where you need to declare variables, modify their values, and perform specific operations.

## Your task

In this challenge, you need to declare two mutable variables inside the function using the `let mut` keyword:

- `x` with an initial value of `5`
- `y` with an initial value of `10`

Then, perform the following operations:

1. Increment `x` by `10`
2. Multiply `y` by `2`

Finally, calculate the sum of `x` and `y` and return the result.

## Requirements

- Declare two mutable variables, `x` and `y`, and assign them initial values.
- Increment `x` by `10`.
- Multiply `y` by `2`.
- Calculate the sum of `x` and `y`.
- Return the calculated sum.

## Example

```rust
let result = manipulate_variables();
assert_eq!(result, 35);
```

## Hints

- Use the let mut keyword to declare mutable variables.
- Use the += operator to increment a variable.
- Use the \*= operator to multiply a variable.
- Calculate the sum of x and y using the + operator.
9 changes: 9 additions & 0 deletions challenges/mutable-variables/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn manipulate_variables() -> i32 {
let mut x = 5;
let mut y = 10;

x += 10;
y += 2;

x + y
}
8 changes: 8 additions & 0 deletions challenges/mutable-variables/src/starter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub fn manipulate_variables() -> i32 {
// TODO: Implement the function here
// Declare x and y as mutable variables
// Increment x by 10
// Multiply y by 2
// Calculate the sum of x and y
// Return the calculated sum
}
48 changes: 48 additions & 0 deletions challenges/mutable-variables/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#[cfg(test)]
mod tests {
use mutable_variables::*;
use syntest::{LocalVariable, Mutation, Syntest, Value};

#[test]
fn test_compiles() {
manipulate_variables();
}

#[test]
fn test_variables() {
let syntest = Syntest::from("./src/lib.rs");

// Expect the 2 variables to exist
syntest
.variables("manipulate_variables")
.iter()
.for_each(|var| match var {
LocalVariable::Int { .. } => match var.name() {
"x" => {
assert_eq!(var.value(), Value::Int(15));
assert_eq!(var.is_used(), true);
assert_eq!(var.is_mutable(), true);
assert_eq!(
var.mutations(),
&vec![Mutation::new(Value::Int(5), Value::Int(15))]
);
}
"y" => {
assert_eq!(var.value(), Value::Int(12));
assert_eq!(var.is_used(), true);
assert_eq!(var.is_mutable(), true);
assert_eq!(
var.mutations(),
&vec![Mutation::new(Value::Int(10), Value::Int(12))]
);
}
_ => {
panic!("Expected an integer variable")
}
},
_ => {
panic!("Expected an integer variable")
}
})
}
}

0 comments on commit 645c41f

Please sign in to comment.