I just have started learning Rust. While going to some of its examples, I could not understand behaviour of references (Which I loosely relate with pointers).
fn main(){
let mut s = String::from("abc");
DoStuff(&mut s);
}
fn DoStuff(reff : &mut String){
reff.push_str("xyz");
(*reff).push_str("xyz");
// In the above two lines, why they perform similar actions?
// as reff and *reff are different types.
// *reff should only be able to call pust.str(). But how reff can also call it?
// reff = String::from("dsf"); --> Why this will give compile error
// if reff & (*reff) behaves somewhat similar for us
}
In the above example, both lines
reff.push_str("xyz");
(*reff).push_str("xyz");
act similar.
As, pust_str()
is a method from String
. Only (*reff)
should be able to call it as per my understanding. Still, reff
is also able to call it.
How can I understand this unexpected behaviour?