Text
RUST: OWNERSHIP & BORROWING
In Rust, we have some concepts that no other lang has; ownership and borrowing are great examples of this. But what are these two features? Well, I'll try to explain:
OWNERSHIP
Imagine you have an apple, and you take this apple to your friend. Now your friend has this apple, and you don't. Basically, this is what Rust does with the variable values when you write something like this:
let a = String::from("example"); let b = a; println!("{}", a);
If you try to run this code, it won't work. But why? Well, basically we just need to read the apple example again, but i gonna explain better why this happens:
Rust has the Copy Trait. This trait makes it possible to copy a variable's value using another variable, just like: let a = 10; let b = a;. In this case, the code won't throw an error because 10 implements the Copy Trait! In summary, the Copy Trait applies to values that are stored in stack memory, and Rust knows their size, making it easier to copy the variable's value. Maybe I'll write about this topic later, but all you need to know for now is this.
Strings aren't part of the Copy Trait; the String value is in Heap memory, so Rust can't automatically know its size, making it impossible to copy. Knowing this, Rust is just going to take the "a" value and transfer it to "b", leaving "a" as an empty variable (just like in the apple example: I no longer own the apple, my friend takes ownership of it).
BORROWING
We learned that a String cannot belong to two different variables at the same time; only one of them can take ownership of it. Well, if you want to make b = a and not lose the value of a, you’re going to need to borrow "a" value to "b". Before the technical explanation, take a look at the example:
I have a pen. I lend this pen to my friend; however, unlike the apple example, my friend will not take ownership of it. He will just borrow it, and I will still be the owner of the pen.
To borrow a value in Rust, you will use & before the variable from which you want to copy the value. See:
let a = String::from("example"); let b = &a; //if you print b, you will see: "example" too println!("{}", a);
Now the code will work because "b" doesn't take ownership of the "a" value; it just point to the "a" value and use it.
We have two possibilities in borrowing:
1- only read the value and point it
2- point to the value and add something to it.
In the example, i used the first possibility; nothing has been changed; "b" just point to "a". If you want to add something to the reference (&a), you need to create a mutable var first (in rust variables are immutable by default), take a look:
fn main() { let mut a = String::from("example"); //"mut" makes "a" mutable modify(&mut a); //calls modify function println!("{}", a); } //function that add a string (str != String) fn modify(new: &mut String){ new.push_str(" cool"); }
Now "a" will be printed like this: "example cool".
I hope it was possible to understand a bit. This is my first post here, and also my first time teaching something like this.
9 notes
·
View notes
Note
saia br eca br odeio os brs horrível
Exposed!!!
3 notes
·
View notes