E3012#
结构体模式和字典模式不能被混用。字典模式中的键必须是字面量,而结构体模式的键必须是字段名。
错误示例#
struct S {
value : Int
}
pub fn S::get(self : S, index : String) -> Int? {
if index == "value" {
return Some(self.value)
}
return None
}
test {
let s : S = { value: 42 }
match s {
{ "value": value, value, .. } => println("Value is: \{value}") // Error: Struct pattern and map pattern cannot be mixed.
_ => println("No value")
}
}
建议#
从模式中移除字典模式部分或结构体模式部分。
///|
pub(all) struct S {
value : Int
}
///|
pub fn S::get(self : S, index : String) -> Int? {
if index == "value" {
return Some(self.value)
}
return None
}
///|
fn use_map_pattern(s : S) -> Unit {
match s {
{ "value": value, .. } => println("Value is: \{value}")
_ => println("No value")
}
}
///|
test {
let s : S = { value: 42 }
use_map_pattern(s)
use_struct_pattern(s)
}
或者,
///|
fn use_struct_pattern(s : S) -> Unit {
match s {
{ value } => println("Value is: \{value}")
}
}