Skip to content
← Back to Community
Rust Mutable References (Garbage Collecton)
Profile icon
Jakman

So there will be a time when you want to borrow the value of a number or string and then return it back to its original owner. Like using a reference to change it and then dumping the reference to return the variable. This is one of the hardest concepts of rust. let me write a script for you to understand

fn main(){ let mut number = 419; // mutable variable called number let pointer = &mut number; *pointer +=1; print("{}",number); }

There's alot here. Let's break it down.
In the first line of the main function we have a mutable vari named number.
In the next line we have a reference to that number called pointer. This is just like saying Rust &mut number
Now we dereference of the pointer variable with the dereference operator * followed by the reference variable. This is just like saying number because by dereferencing the reference we go back to what the reference was pointing to. So any way we increment the pointer variable by 1 and in doing so we increment it's true value (number). In the last line we print out the number. This will print out 420.
Here are some tips about references in rust. They are not like they are in C,C++,C#. You cannot just have multiple mutable refrences to the same variable. That makes things run slower and Rust will not have that because that is not safe.
If you wanted multiple references you would do this

fn main(){ let mut number = 419; { let pointer1 = &mut number; *pointer1 += 1; print!("{}",number); } /* pointer1 mutable ref goes out of scope and number is returned back to number*/ { let pointer2 = &mut number; // new refrence *pointer2 += 1; print!("{}",number); } /* number is printed and refrence number is returned back to number */ }

So because more than one mutable refrence can not be made in the same scope, you have to make a refrence in a different code block (made by closed curly brackets.) and then you have to only use that refrence in that code block. That refrence cannot be used after the code block ends. Then you can make as many as you want to in the same form. For the last mutable reference you can choose not to encase it in a code block and it will still work as long as you do not make another one.
One last thing. The dereference operator used because other than that the refrence is only equal to the place in memory that it refrences. Also if refrences are not used errors will occur.
Thank you for reading my Ted talk. I am going back to my cave.

Voters
Profile icon
PavelSalajka
Profile icon
dabs364
Profile icon
Highwayman
Profile icon
coderash
Profile icon
ARJPEG
Profile icon
HahaYes
Profile icon
NoelB33
Profile icon
AmazingMech2418
Profile icon
Jakman
Comments
hotnewtop
Profile icon
AmazingMech2418

Nice! Mutable references definitely come in handy for structs with methods! Though, you don't need to use *pointer1 in Rust; you can just use pointer1. The * isn't needed in Rust.

Profile icon
Jakman

@AmazingMech2418 actually they are because other wise the refrence is still pointing to the memory. Run my second program and then after that take the pointers out.

Profile icon
AmazingMech2418

@Jakman Oh! That's needed for dereferencing...

Profile icon
Jakman

@AmazingMech2418 yeah man. Derefrencing is a little different than in a C language.

Profile icon
AmazingMech2418

@Jakman Yeah. Though, pointers can be used in Rust without the *, just not in all cases.

Profile icon
Jakman

@AmazingMech2418 yeah. They have their uses in programming unlike other languages cough cough C#

Profile icon
Jakman

@AmazingMech2418 when you are manipulating the value then it is to be refrenced as a pointer. Other than that, leave it be.

Profile icon
HahaYes

where is your cave?

Profile icon
Jakman

@HahaYes in the universe of the internet.

Profile icon
HahaYes
Profile icon
HahaYes

pretty cool!

Profile icon
Jakman

@HahaYes thanks.

Profile icon
HahaYes

@Jakman you are welcome

Profile icon
Jakman

@HahaYes 355 upvotes already? i see u.

Profile icon
HahaYes

@Jakman wdym

Profile icon
HahaYes

@Jakman I literally do not know where all these upvotes are coming from

Profile icon
Jakman

@HahaYes oh he might be me

Profile icon
HahaYes

@Jakman ? I'm confused

Profile icon
Jakman

@HahaYes lol. idk either You gained fast tho.

Profile icon
HahaYes

@Jakman Yeah, I think it is all the comments

Profile icon
Jakman

@HahaYes me too.

Profile icon
HahaYes

@Jakman anyways, gtg, see ya

Profile icon
Jakman

@HahaYes later