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.
text
with any initial value of type String
Use let mut
to make it mutable.text
to something else of your choice.mutates_value(&mut text)
function, which will change the value of your text
variable.If you're stuck, feel free to check out the hints below
Use the let mut
keyword to declare a mutable variable.
To give the value to as a mutable reference use the &mut
keyword. e.g.
let mut text = "hello";
mutates_value(&mut text);
To create a String
from a &str
, use String::from(value)
or value.to_string()
. e.g.
let text = "hello";
let text = text.to_string();
To return a value from a function, either use the return
statement or let the last expression be the return value. For example:
// Using return statement
fn example1() -> String {
return String::from("hello");
}
// Using expression (no semicolon)
fn example2() -> String {
String::from("hello")
}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String let mut text = "hello".to_string(); mutates_value(&mut text); return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text:String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("value"); mutates_value(&mut text); return text // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye");}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text.to_string();}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { let mut text = String::from("hello"); mutates_value(&mut text); return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("bye"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { let mut text = String::from("hello"); mutates_value(&mut text); text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text.to_string()}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}fn main() { // 4. Call `mutating_variables` and print the result let result = mutating_variables(); println!("{}", result);}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String String::from(text)}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text.to_string()}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String let mut text = "hello".to_string(); text = "qc".to_string(); mutates_value(&mut text); return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut hello_str = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut hello_str); // 3. Return the value of `text` as a String return hello_str;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut hello_str = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut hello_str); // 3. Return the value of `text` as a String return hello_str;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { let mut text = "hello".to_string(); mutates_value(&mut text); return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value( &mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String let mut text: String = "hello".to_string(); mutates_value(&mut text); return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return String::from(text);}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let text = "hello"; let mut text = text.to_string(); mutates_value(&mut text); return String::from(text); // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { let mut input = String::from("foobar"); mutates_value(&mut input); input}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String let mut text = String::from("hello"); mutates_value(&mut text); text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); mutates_value(&mut text); return text; // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a String}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text.to_string()}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { let mut text = "hello".to_string(); mutates_value(&mut text); text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" // 2. Call `mutates_value` with a mutable reference to `text` // 3. Return the value of `text` as a Stringlet mut text = "hello".to_string();mutates_value( & mut text);text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text : String = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text= String:: from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("helllo"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text: String = String::from("helllo"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = "hello".to_string(); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String text}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}
pub fn mutating_variables() -> String { // 1. Declare a mutable variable `text` with value "hello" let mut text = String::from("hello"); // 2. Call `mutates_value` with a mutable reference to `text` mutates_value(&mut text); // 3. Return the value of `text` as a String return text;}// Do not change this functionpub fn mutates_value(value: &mut String) { *value = String::from("bye")}