E4184#
Compiler diagnostic name: start_rest_binder_is_not_supported_in_longest_match_strategy.
最长匹配策略的 lexmatch 不支持开头的剩余绑定器。
在 lexmatch ... with longest 中,引擎会选择最长的匹配分支。允许在正则前出现剩余绑定器会让这个选择变得含糊,因此开头的剩余绑定器会被拒绝。
错误示例#
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
(_, "abc", _) => ()
_ => ()
}
}
第一个 _ 试图在 "abc" 正则模式之前绑定或跳过输入。
修改建议#
让正则从剩余输入的开头开始匹配,并且只在需要时绑定后缀。
///|
pub fn match_text(text : String) -> Unit {
lexmatch text with longest {
("abc", _) => ()
_ => ()
}
}