このプログラムを、ビルドすると所有権がらみのエラーになります。
エラーメッセージにある通りに、一旦、ローカル変数を経由して、2行に分けるだけで、解決してしまいます。。汗
なぜ?
fn main() {
let mut h = Hoge { hoge2 : 0 };
h.hoge3();
}
struct Hoge {
hoge2:usize,
}
impl Hoge {
fn get_byte(&mut self)->u8 { 0
}
fn format_byte(&mut self , address:u8 ) ->String {
let s = format!("{:02X}H",address);
s
}
fn hoge3(&mut self) {
println!( "{}",format!("OR {}",self.format_byte(self.get_byte() ))); // A
//let a = self.get_byte(); // B
//println!( "{}",format!("OR {}",self.format_byte( a ))); // B
}
}
⣿
Standard Error
Compiling playground v0.0.1 (/playground)
error[E0499]: cannot borrow `*self` as mutable more than once at a time
--> src/main.rs:19:60
|
19 | println!( "{}",format!("OR {}",self.format_byte(self.get_byte() ))); // A
| -----------------^^^^^^^^^^^^^^^--
| | | |
| | | second mutable borrow occurs here
| | first borrow later used by call
| first mutable borrow occurs here
|
help: try adding a local storing this argument...
--> src/main.rs:19:60
|
19 | println!( "{}",format!("OR {}",self.format_byte(self.get_byte() ))); // A
| ^^^^^^^^^^^^^^^
help: ...and then using that local as the argument to this call
--> src/main.rs:19:43
|
19 | println!( "{}",format!("OR {}",self.format_byte(self.get_byte() ))); // A
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0499`.
error: could not compile `playground` due to previous error
Standard Output