E0077

E0077#

Warning name: lexmatch_longest_match

此警告描述的是早期实验性的最长匹配 lexmatch 形式,当前编译器不会发出该警告。

当前支持对内存中的 StringStringView 输入使用 lexmatch ... with longest,其分支须使用正则常量表达式。

错误示例#

///|
fn classify(input : String) -> String {
  lexmatch input with longest {
    ("if|[a-z]*" as token) => token.to_owned()
    _ => "other"
  }
}

///|
test {
  inspect(classify("iff"), content="iff")
}

建议#

将每个旧式字符串模式转换为正则字面量,并使用 ^ 锚定开头;当分支必须消费整个输入时,还需使用 $ 锚定结尾。将末尾的剩余部分绑定改为 after=

///|
fn classify(input : String) -> String {
  lexmatch input with longest {
    re"^(if|[a-z]*)$" as token => token.to_owned()
    _ => "other"
  }
}

///|
test {
  inspect(classify("iff"), content="iff")
}