error[E0596]: cannot borrow immutable local variable `x` as mutable
--> src/main.rs:32:18
|
31 | let x = 5;
| - consider changing this to `mut x`
32 | let y = &mut x;
| ^ cannot borrow mutably
use std::cell::RefCell;
fnmain() {
let x = RefCell::new(5u8);
assert_eq!(5, *x.borrow());
{
letmut y = x.borrow_mut();
*y = 10;
assert_eq!(10, *x.borrow());
let z = x.borrow(); //编译时会通过,但运行时panic!
}
}
运行结果:
thread 'main' panicked at 'already mutably borrowed: BorrowError', libcore/result.rs:983
:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.