我想检查字符串是否包含'$'以及'$'后面是否有内容:
我试过这段代码:
fn test(s: String) {
match s.find('$') {
None | (Some(pos) if pos == s.len() - 1) => {
expr1();
}
_ => { expr2(); }
}
}
Run Code Online (Sandbox Code Playgroud)
但它没有编译:
Run Code Online (Sandbox Code Playgroud)error: expected one of `)` or `,`, found `if`
它是不可能的结合None和Some比赛时手臂?
如果是这样,expr1()除非将其移动到单独的函数中,否则有一种简单的方法可以不复制?
将匹配保护(thingy)仅适用于一种模式替代(用符号分隔的东西)是不可能的.每只手臂只有一个防护装置,它适用于该手臂的所有模式.if|
但是,有许多解决方案可以解决您的具体问题.例如:
if s.find('$').map(|i| i != s.len() - 1).unwrap_or(false) {
expr2();
} else {
expr1();
}
Run Code Online (Sandbox Code Playgroud)