6
मैं ढेर पर एक परिवर्तनीय संरचना बनाना चाहता हूं और इसे सहायक कार्यों से बदलना चाहता हूं।मैं जंग में म्यूटेबल डेटा के संदर्भ को कैसे पास करूं?
#[deriving(Show)]
struct Game {
score: u32,
}
fn addPoint (game: &mut Game) {
game.score += 1;
}
fn main() {
let mut game = Game { score: 0 };
println!("Initial game: {}", game);
// This works:
game.score += 1;
// This gives a compile error:
addPoint(&game);
println!("Final game: {}", game);
}
इस देता संकलित करने के लिए कोशिश कर रहा है:
rustc mutable.rs
mutable.rs:19:18: 19:23 error: cannot borrow immutable dereference of `&`-pointer as mutable
mutable.rs:19 addPoint(&game);
^~~~~
error: aborting due to previous error
क्या मैं गलत कर रहा हूँ?
कोशिश 'addPoint (और mut खेल)'। –
एफवाईआई: सम्मेलन में 'एफएन एडपॉइंट (गेम: और म्यूट गेम)' के बजाय 'एफएन एड_पॉइंट (गेम: एंड म्यूट गेम)' होगा। –
@ क्रिस मॉर्गन मैं जंग आदमी से प्यार करता हूं, यहां तक कि, उन्हें यह सही मिला (मेरे स्वाद के लिए आखिरी बार) –