Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何捕获可变变量?_Rust - Fatal编程技术网

Rust 如何捕获可变变量?

Rust 如何捕获可变变量?,rust,Rust,如何捕获可变变量 我也尝试过使用这些函数和方法,但后来一切都崩溃了,因为它试图将自己移动到闭包中,就像 struct Level{ i_vec: ~[int] } pub struct GameManager{ lvl: Level } impl GameManager { pub fn new() -> GameManager{ GameManager {lvl: Level{i_vec: ~[]}} } pub fn new_ga

如何捕获可变变量

我也尝试过使用这些函数和方法,但后来一切都崩溃了,因为它试图将自己移动到闭包中,就像

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;    
}
是否有我可以使用的自参数?因为通用汽车公司本身应该在关闭时可用

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;    
}

您遇到的问题是无法在两个任务之间直接共享内存。处理此问题的传统方法是将数据移动到线程。下面是一个例子:

do gm.default_game_loop(){
        let level = self.lvl;    
}
使用std::task;
结构级{
i_vec:~[int]
}
发布结构游戏管理器{
lvl:水平
}
impl游戏管理器{
pub fn new()->GameManager{
游戏管理器{lvl:Level{i_vec:~[]}
}
pub fn新游戏频道,或通过互斥保护