繁体   English   中英

如何检查字符串是否包含Rust中的子字符串?

[英]How to check if a string contains a substring in Rust?

我正在尝试查找子字符串是否在字符串中。 在Python中,这涉及到in经营者,所以我写了这样的代码:

let a = "abcd";
if "bc" in a {
    do_something();
}

我收到一条奇怪的错误消息:

error: expected `{`, found `in`
 --> src/main.rs:3:13
  |
3 |       if "bc" in a {
  |  _____________-^
4 | |         do_something();
5 | |     }
  | |_____- help: try placing this code inside a block: `{ a <- { do_something(); }; }`

消息表明我把它放在一个区块中,但我不知道该怎么做。

Rust没有这样的操作员。 您可以使用String::contains方法

if a.contains("bc") {
    do_something();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM