I just started learning rust. I was following the rust docs. I wrote a program and I am not sure why it works.
According to borrow rules, either I can have one mutable reference in a scope or any number of mutable reference but never both.
So, I cannot have a mutable reference and another immutable reference in the same scope.
But, my program works.
Here is my code -
fn main() {
let s = String::from("Hi");
let mut s = random(s);
change_string(&mut s);
print(&s);
let d = &mut s;
change_string(d);
let f = &s;
print(f);
// print(d);
// change_string(d);
}
fn random(mut s: String) -> String {
s.push_str("rando");
print(&s);
s
}
fn change_string(s: &mut String) {
s.push_str("Hello");
}
fn print(s: &String) {
println!("{s}");
}