Pointers 如何在Rust中传递对可变数据的引用?

Pointers 如何在Rust中传递对可变数据的引用?,pointers,mutable,rust,Pointers,Mutable,Rust,我想在堆栈上创建一个可变结构,并从helper函数中对其进行变异 #[derive(Debug)] 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 wo

我想在堆栈上创建一个可变结构,并从helper函数中对其进行变异

#[derive(Debug)]
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);
}
尝试编译此文件将提供:

错误[E0308]:类型不匹配
-->src/main.rs:19:14
|
19 |附加点(和游戏);
|^^^^^^类型的可变性不同
|
=注意:预期类型为`&mut游戏`
找到类型`&游戏`

我做错了什么?

引用也需要标记为可变:

addPoint(&mut game);

仅供参考:大会将有
fn添加点(游戏:&mut游戏)
而不是
fn添加点(游戏:&mut游戏)