E3012

E3012#

结构体模式和字典模式不能被混用。字典模式中的键必须是字面量,而结构体模式的键必须是字段名。

错误示例#

struct S {
  value : Int
}

pub fn S::op_get(self : S, index : String) -> Int? {
  if index == "value" {
    return Some(self.value)
  }
  return None
}

fn main {
  let s : S = { value: 42 }
  match s {
    { "value": value, value } => println("Value is: \{value}") // Error: Record pattern and map pattern cannot be mixed.
    _ => println("No value")
  }
}

建议#

从模式中移除字典模式部分或结构体模式部分。

fn main {
  let s : S = { value: 42 }
  match s {
    { "value": value } => println("Value is: \{value}")
    _ => println("No value")
  }
}

或者,

fn main {
  let s : S = { value: 42 }
  match s {
    { value } => println("Value is: \{value}")
  }
}