struct Level{
i_vec: ~[int]
}
pub struct GameManager{
lvl: Level
}
impl GameManager {
pub fn new() -> GameManager{
GameManager {lvl: Level{i_vec: ~[]}}
}
pub fn new_game(f: ~fn()) {
do spawn {
f();
}
}
pub fn default_game_loop(lvl: &Level ,f: &fn() ){
loop {
f();
break;
}
}
}
fn main() {
let mut gm = GameManager::new();
do GameManager::new_game(){
// I know I could move "gm" here, but I would like
// to know how to capture mutable variables.
do GameManager::default_game_loop(&gm.lvl){
}
}
}
/*
/home/maik/source/test.rs:28:43: 28:45 error: mutable variables cannot be implicitly captured
/home/maik/source/test.rs:28 do GameManager::default_game_loop(&gm.lvl){
^~
error: aborting due to previous error
[Finished in 0.2s with exit code 101]
*/
如何捕获可变变量?
我也尝试让这些函数成为方法,但后来一切都崩溃了,因为它试图将自己移动到闭包中
do gm.default_game_loop(){
let level = &gm.lvl;
}
有我可以使用的 self 参数吗?因为 gm 本身应该在闭包中可用
do gm.default_game_loop(){
let level = self.lvl;
}