如果字符串包含如何匹配?

jon*_*rdl 2 pattern-matching rust

我有一个字符串变量“distro”

let distro = get_distro().unwrap().name;
Run Code Online (Sandbox Code Playgroud)

该变量的值例如是“Arch Linux”。
现在我想检查一个匹配,如果变量包含“arch”。

let distro = get_distro().unwrap().name;
Run Code Online (Sandbox Code Playgroud)

Bra*_*man 6

要在 rust 匹配中使用条件,您需要向匹配添加 if 。在这种情况下,它看起来像

match &distro.to_lowercase() {
  x if x.contains("arch") => //...
}
Run Code Online (Sandbox Code Playgroud)