-
Notifications
You must be signed in to change notification settings - Fork 20
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
132 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,7 @@ | ||
[package] | ||
name = "mutable-variables" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[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,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. |
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 @@ | ||
pub fn manipulate_variables() -> i32 { | ||
let mut x = 5; | ||
let mut y = 10; | ||
|
||
x += 10; | ||
y += 2; | ||
|
||
x + y | ||
} |
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,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 | ||
} |
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,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") | ||
} | ||
}) | ||
} | ||
} |