Rust for beginners — Part 6 — String
Let’s look into how we can use strings in rust
Note: Kindly have a look into the previous section of tutorial to understand about stack and heap in rust
Previous section: https://medium.com/@manikandan96372/rust-for-beginners-part-5-stack-and-heap-ce5b8a0076d6
fn main() {
let a = String::from("Hello");
println!("{}", a);
}
The above code snippet on execution would give the following output
Hello
In rust we define a string using the String keyword along with from, and the string value that is to be assigned to the variable, “hello” in this case.
Now let’s see how we can assign this string variable to another variable
fn main() {
let a = String::from("Hello");
let b = a; println!("{}", b);
}
The above code snippet on execution would give the following output
Hello
This looks like a straight forward approach, where we define variable “a” initially with “Hello” as the value and then variable “a” is assigned to variable “b”, when variable b is printed we get the value “Hello” as a result.
Now, let’s try modifying the variable “b” by pushing another string value to it
fn main() {let a = String::from("hello");
let b = a;b.push_str(" world!");println!("{}", b);
}
The above code snippet on execution would give the following error
This error clearly states that we have to declare our variable “b” as mutable, only then it’s value could be modified. Let’s try that
fn main() {let a = String::from("Hello");
let mut b = a;b.push_str(" World!");println!("{}", b);
}
The above code snippet on execution would give the following output
Hello World!
This is what we expected, As we are already printing the value of “b”, now let’s try printing the value of “a”
fn main() {let a = String::from("Hello");
let mut b = a;b.push_str(" World!");println!("{}", b);println!("{}", a);
}
The above code snippet on execution would give the following error
But why ? The code looks like both variable “a” and “b” has their values, but when printing variable a why does it throw an error ?
This is one of the interesting aspects of rust lang, what happens on the above code is,
- Variable “a” gets defined with a string value “Hello”
- Variable “b” is declared as a mutable variable and variable “a” is assigned to it
- Now what rust does is when you assign variable a to variable “b”, rust does not just take a copy of the value of variable “a” and assign to “b”
- Since variable “a” is a string and it’s value is considered to be dynamic, the value of variable “a” is stored in the memory heap, and address of that value will be stored in the stack where variable “a” resides.
- When variable “a” is assigned to variable “b”, this is not just assigning the value but we are moving the address pointer of variable “a” to variable “b”
- Which means the address of the value of variable “a” in memory heap will now be moved to variable “b” and variable “a” will no longer hold the address of the string value “hello” in the stack.
- That’s why when you move one variable’s value to another, you can no longer access the previous variable as you have moved the entire variable value to another and the previous variable will no longer hold the value
- So variable “a” cannot be used elsewhere after moving it to variable “b”
Now we understood that how strings work in rust, but it is a very common scenario that we will be needing both variable a and b as accessible ones in real-time programming, so how do we handle it ? Let’s discuss that in the next section !