Is there a way to have a Rust closure that moves only some variables into it?

后端 未结 2 1419
小蘑菇
小蘑菇 2020-12-10 07:49

I have a general struct with settings and an extra variable setting that I want to tune and play around with.

For all possible values in an integer ra

2条回答
  •  借酒劲吻你
    2020-12-10 08:23

    The closure! macro gives the ability to selectively reference, move, or clone variables into a closure.

    Example taken from the docs:

    use closure::closure;
    
    let string = "move".to_string();
    let x = 10;
    let mut y = 20;
    let rc = Rc::new(5);
    
    let closure = closure!(move string, ref x, ref mut y, clone rc, |arg: i32| {
        ...
    });
    

    Variables that are captured but not listed default to being moved.

提交回复
热议问题