From 0e06b4258adbeeae93f9fde430e4f4d1753a0600 Mon Sep 17 00:00:00 2001 From: dcodesdev <101001810+dcodesdev@users.noreply.github.com> Date: Sat, 8 Jun 2024 19:21:00 +0300 Subject: [PATCH] mutable vars update --- challenges/mutable-variables/description.md | 10 ++++++---- challenges/mutable-variables/src/lib.rs | 4 ++-- challenges/mutable-variables/tests/tests.rs | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/challenges/mutable-variables/description.md b/challenges/mutable-variables/description.md index ae4d305..1505592 100644 --- a/challenges/mutable-variables/description.md +++ b/challenges/mutable-variables/description.md @@ -9,9 +9,9 @@ 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 @@ -19,12 +19,14 @@ In this challenge, you need to declare one mutable variable inside the function 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. diff --git a/challenges/mutable-variables/src/lib.rs b/challenges/mutable-variables/src/lib.rs index 5e08e91..a834060 100644 --- a/challenges/mutable-variables/src/lib.rs +++ b/challenges/mutable-variables/src/lib.rs @@ -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 } diff --git a/challenges/mutable-variables/tests/tests.rs b/challenges/mutable-variables/tests/tests.rs index 9fde101..9a0bcbb 100644 --- a/challenges/mutable-variables/tests/tests.rs +++ b/challenges/mutable-variables/tests/tests.rs @@ -56,12 +56,12 @@ mod tests { match ¯os[..] { [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!" @@ -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!"); } _ => {