Skip to content

Commit

Permalink
mutable vars update
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodesdev committed Jun 8, 2024
1 parent 4dd6b62 commit 0e06b42
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
10 changes: 6 additions & 4 deletions challenges/mutable-variables/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ In this challenge, you will declare and use **mutable variables in Rust**. You w
In this challenge, you need to declare one mutable variable inside the function using the `let mut` keyword:

- `text` with an initial value of `"hello"`
- Print it to the console using the `println!` macro.
- Print exactly `"Text is hello"` to the console using the `println!` macro.
- Mutate the variable to `"bye"`.
- Print the variable again to the console using the `println!` macro.
- Print exactly `"Text is bye"` to the console using the `println!` macro.
- Return the final value of the variable.

### Println! Macro

The `println!` macro is used to print text to the console. It is similar to the `println` function in other programming languages. The `println!` macro is used to print formatted text to the console.

```rust
println!("Hello, World!");
let text = "hello";
println!("Text is {}", text);
```

The `{}` is a placeholder that will be replaced by the value of the variable `text`.

## Hints

- Use the `let mut` keyword to declare a mutable variable.
- Reassign the variable directly by using the `=` operator.
- Ensure to return the final value of the variable from the function.
- Use `println!` statements to debug and check the values during reassignment.
4 changes: 2 additions & 2 deletions challenges/mutable-variables/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub fn mutating_variables() -> &'static str {
let mut text = "hello";

println!("Text is: {}", text);
println!("Text is {}", text);

text = "bye";

println!("Text is: {}", text);
println!("Text is {}", text);

text
}
8 changes: 4 additions & 4 deletions challenges/mutable-variables/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ mod tests {

match &macros[..] {
[first, second] => {
// First macro should be println!("Text is: {}", "hello");
// First macro should be println!("Text is {}", "hello");
assert_eq!(first.tokens.len(), 3);
// will have 3 tokens: &str, , and "hello"
match &first.tokens[..] {
[text_is, _comma, text] => {
assert_eq!(text_is, "\"Text is: {}\"");
assert_eq!(text_is, "\"Text is {}\"");
assert_eq!(
text, "text",
"Expected text variable to be used in println!"
Expand All @@ -75,12 +75,12 @@ mod tests {
}
}

// Second macro should be println!("Text is: {}", "bye");
// Second macro should be println!("Text is {}", "bye");
assert_eq!(second.tokens.len(), 3);
// will have 3 tokens: &str, , and "bye"
match &second.tokens[..] {
[text_is, _comma, bye] => {
assert_eq!(text_is, "\"Text is: {}\"");
assert_eq!(text_is, "\"Text is {}\"");
assert_eq!(bye, "text", "Expected text variable to be used in println!");
}
_ => {
Expand Down

0 comments on commit 0e06b42

Please sign in to comment.