测试 Rust 字符串是否包含在字符串数组中
Testing if Rust string is contained in an array of strings
我是 Rust 初学者,我正在尝试扩展当前测试字符串与另一个字符串文字是否相等的条件,以便现在测试该字符串是否包含在字符串文字数组中。
在Python中,我会写string_to_test in ['foo','bar']
。我怎样才能将它移植到 Rust?
这是我的尝试,但无法编译:
fn main() {
let test_string = "foo";
["foo", "bar"].iter().any(|s| s == test_string);
}
有错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `&str` with `str`
--> src/main.rs:3:35
|
3 | ["foo", "bar"].iter().any(|s| s == test_string);
| ^^ no implementation for `&str == str`
|
= help: the trait `PartialEq<str>` is not implemented for `&str`
= note: required because of the requirements on the impl of `PartialEq<&str>` for `&&str`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
不幸的是,我无法解决这个问题,也无法在 Whosebug 或论坛上找到类似的问题。
我是 Rust 初学者,我正在尝试扩展当前测试字符串与另一个字符串文字是否相等的条件,以便现在测试该字符串是否包含在字符串文字数组中。
在Python中,我会写string_to_test in ['foo','bar']
。我怎样才能将它移植到 Rust?
这是我的尝试,但无法编译:
fn main() {
let test_string = "foo";
["foo", "bar"].iter().any(|s| s == test_string);
}
有错误:
Compiling playground v0.0.1 (/playground)
error[E0277]: can't compare `&str` with `str`
--> src/main.rs:3:35
|
3 | ["foo", "bar"].iter().any(|s| s == test_string);
| ^^ no implementation for `&str == str`
|
= help: the trait `PartialEq<str>` is not implemented for `&str`
= note: required because of the requirements on the impl of `PartialEq<&str>` for `&&str`
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` due to previous error
不幸的是,我无法解决这个问题,也无法在 Whosebug 或论坛上找到类似的问题。