我想遍历一个句子以从字符串中提取简单的单词。这里是我到目前为止,试图在输入字符串中parse
功能第一场比赛world
:如何遍历Rust中字符串中的字符以匹配单词?
fn parse(input: String) -> String {
let mut val = String::new();
for c in input.chars() {
if c == "w".to_string() {
// guessing I have to test one character at a time
val.push_str(c.to_str());
}
}
return val;
}
fn main() {
let s = "Hello world!".to_string();
println!("{}", parse(s)); // should say "world"
}
什么是通过字符串中的字符匹配鲁斯特的模式(如迭代的正确方法一个基本的解析器)?
请注意,您的示例不能从tip中编译最新的Rust。使用'c =='w''来比较字符。 – BurntSushi5