^^ help: if this is intentional, prefix it with an underscore: _r1
= note: #[warn(unused_variables)] on by default
error[E0502]: cannot borrow s as immutable because it is also borrowed as mutable
--> src/main.rs:17:19
16
let r2 = &mut s;
------ mutable borrow occurs here
17
println!("{}",s);
^ immutable borrow occurs here
18
19
println!("{} ", r2);
-- mutable borrow later used here
= note: this error originates in the macro $crate::format_args_nl which comes from the expansion of the macro println (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try rustc --explain E0502.
warning: playground (bin "playground") generated 1 warning
error: could not compile playground (bin "playground") due to previous error; 1 warning emitted
🌟🌟
在学习引用与借用的时候,我有一点搞不明白: fn main() { let mut s = String::from("hello, "); let r1 = &mut s; println!("{}",s); // 为什么这里打印s可以 let r2 = &mut s; println!("{}",s); // 为什么这里打印s报错 println!("{}", r2); // 这里打印r2不报错 }
r1
--> src/main.rs:14:9_r1
= note:
#[warn(unused_variables)]
on by defaults
as immutable because it is also borrowed as mutable --> src/main.rs:17:19= note: this error originates in the macro
$crate::format_args_nl
which comes from the expansion of the macroprintln
(in Nightly builds, run with -Z macro-backtrace for more info)For more information about this error, try
rustc --explain E0502
. warning:playground
(bin "playground") generated 1 warning error: could not compileplayground
(bin "playground") due to previous error; 1 warning emitted 🌟🌟