From 645c41fa0068b0d448a1bb32d3f39ed6a4da6a62 Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:39:01 +0300 Subject: [PATCH] mutable vars --- Cargo.lock | 7 +++ challenges/challenges.json | 12 ++++++ challenges/mutable-variables/Cargo.toml | 7 +++ challenges/mutable-variables/description.md | 41 ++++++++++++++++++ challenges/mutable-variables/src/lib.rs | 9 ++++ challenges/mutable-variables/src/starter.rs | 8 ++++ challenges/mutable-variables/tests/tests.rs | 48 +++++++++++++++++++++ 7 files changed, 132 insertions(+) create mode 100644 challenges/mutable-variables/Cargo.toml create mode 100644 challenges/mutable-variables/description.md create mode 100644 challenges/mutable-variables/src/lib.rs create mode 100644 challenges/mutable-variables/src/starter.rs create mode 100644 challenges/mutable-variables/tests/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 5b36271..412d141 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -736,6 +736,13 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mutable-variables" +version = "0.1.0" +dependencies = [ + "syntest", +] + [[package]] name = "native-tls" version = "0.2.11" diff --git a/challenges/challenges.json b/challenges/challenges.json index ad6bfc8..cd45453 100644 --- a/challenges/challenges.json +++ b/challenges/challenges.json @@ -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", diff --git a/challenges/mutable-variables/Cargo.toml b/challenges/mutable-variables/Cargo.toml new file mode 100644 index 0000000..d6f9c90 --- /dev/null +++ b/challenges/mutable-variables/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "mutable-variables" +version = "0.1.0" +edition = "2021" + +[dev-dependencies] +syntest = { path = "../../crates/syntest" } diff --git a/challenges/mutable-variables/description.md b/challenges/mutable-variables/description.md new file mode 100644 index 0000000..bdc52dc --- /dev/null +++ b/challenges/mutable-variables/description.md @@ -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. diff --git a/challenges/mutable-variables/src/lib.rs b/challenges/mutable-variables/src/lib.rs new file mode 100644 index 0000000..03404aa --- /dev/null +++ b/challenges/mutable-variables/src/lib.rs @@ -0,0 +1,9 @@ +pub fn manipulate_variables() -> i32 { + let mut x = 5; + let mut y = 10; + + x += 10; + y += 2; + + x + y +} diff --git a/challenges/mutable-variables/src/starter.rs b/challenges/mutable-variables/src/starter.rs new file mode 100644 index 0000000..5982a30 --- /dev/null +++ b/challenges/mutable-variables/src/starter.rs @@ -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 +} diff --git a/challenges/mutable-variables/tests/tests.rs b/challenges/mutable-variables/tests/tests.rs new file mode 100644 index 0000000..8fae6e7 --- /dev/null +++ b/challenges/mutable-variables/tests/tests.rs @@ -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") + } + }) + } +}