Doubt about borrow rules

Why if I write (case 1)

let i = String::from("sample");
println! ("{}", i);
println! ("{}", i);

compiles fine but,(case 2)

let i = String::from("sample");
some_function(i);
println! ("{}", i);

It gives the borrow error. The second part I understood it. It is violating the borrow rules in the second part. But, in case 1, The first println! the variable i is borrowed. seems like error should be thrown but, I think my guess println! is not like an actual function. Can anyone explain what's going on here?

println! is a macro, so it's allowed to do weird stuff. In this case, it implicitly adds an & to every variable that gets passed to it.

2 Likes

Thanks for the solution

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.