Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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
Pattern matching 无法通过移动到图案保护中进行绑定_Pattern Matching_Rust - Fatal编程技术网

Pattern matching 无法通过移动到图案保护中进行绑定

Pattern matching 无法通过移动到图案保护中进行绑定,pattern-matching,rust,Pattern Matching,Rust,如何通过移动到s上的模式保护[E0008]来解决无法绑定的问题 let res = match Some("hi".to_string()) { Some(s) if s.len() == 0 => 1, _ => 3 }; 是否可以在不将条件放入arm的情况下更改它?在这种情况下,您可以通过引用绑定: let res = match Some("hi".to_string()) { Some(ref s) if s.len() == 0 => 1,

如何通过移动到
s
上的模式保护[E0008]来解决
无法绑定的问题

let res = match Some("hi".to_string()) {
    Some(s) if s.len() == 0 => 1,
    _ => 3
};

是否可以在不将条件放入arm的情况下更改它?

在这种情况下,您可以通过引用绑定:

let res = match Some("hi".to_string()) {
    Some(ref s) if s.len() == 0 => 1,
    _ => 3
};
这里的一般问题是,move绑定必须禁止进一步使用原始变量,因为移出会使数据无效。如果防护为
false
,则需要使用原始变量与后面的模式进行匹配,因为移动是非法的

例如:

fn f(x: Option<String>) {
    match x {
        Some(a) if { drop(a); false } => println!("impossible"),
        Some(b) => println!("whoops, {}", b),
        None => println!("none"),
    }
}
fn f(x:选项){
匹配x{
一些(a)如果{drop(a);false}=>println!(“不可能”),
一些(b)=>println!(“哎呀,{},b),
无=>println!(“无”),
}
}

如果
x
Some
,则在决定是否应采用
a
arm时,内部
字符串将移出并释放,但一旦
a
arm被拒绝,相同的
字符串将立即再次用于
b
arm。

因此,基本上编译器是这么说的“嘿,我很乐意将‘e’移到守卫语句中,但如果守卫语句失败,我就无法将‘e’移回。因此,唯一可行的方法是你让我借用“e”来检查守卫声明。“如果我不需要守卫表达式中的所有权,但需要在身体中的所有权怎么办?@Thayne显然你需要使用Rust 2018 edition和
![功能(按移动模式绑定守卫)]
,等待完全执行,或将防护装置移入火柴体内。RFC现在稳定: