Rust 即使在解释时也无法推断值类型
我需要在空格和其他几个字符上拆分一个字符串Rust 即使在解释时也无法推断值类型,rust,Rust,我需要在空格和其他几个字符上拆分一个字符串 let mut text = String::from("foo,bar baz"); for word in text.split(|c| c.is_whitespace::<char>() || c == ',' ).filter(|&s| !s.is_empty()) { println!("{}", word); } let mut text=String::from(“foo,bar baz”); 对于文本中的单
let mut text = String::from("foo,bar baz");
for word in text.split(|c| c.is_whitespace::<char>() || c == ',' ).filter(|&s| !s.is_empty()) {
println!("{}", word);
}
let mut text=String::from(“foo,bar baz”);
对于文本中的单词。拆分(|c | c.is_空格::()| | c==','))。筛选器(|&s |!s.is_empty()){
println!(“{}”,word);
}
然而,编译器说:
错误:此值的类型在此上下文中必须是已知的
-->src/main.rs:4:32
|
4 |用于文本中的单词。拆分(| c | c.is_空格::()| | c==','))。筛选器(|&s |!s.is_empty()){
| ^^^^^^^^^^^^^^^^^^^^^^^^^
我做错了什么?为什么它不能推断类型?该方法接受任何满足
模式或|c | char::is_空格(c)的内容| | c==','
-无论出于何种原因,我不喜欢在闭包参数列表中指定类型。公平。是否有类似的社区指导原则?我不认为有任何针对这种特殊情况的指导原则;这只是我纯粹的偏好。Rustfmt是我正式风格的正常选择。
impl<'a, F> Pattern<'a> for F
where
F: FnMut(char) -> bool,
text.split(|c| c.is_whitespace::<char>() || c == ',' )
text.split(|c: char| c.is_whitespace() || c == ',' )