为什么会这样
fn main() {
let test = "5% of foo".to_string();
let result: i32 = test.split('%').collect()[0].parse().unwrap_or(0);
}
导致错误
error[E0282]: type annotations needed
--> src/main.rs:4:23
|
4 | let result: i32 = test.split('%').collect()[0].parse().unwrap_or(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `B`
这也无济于事:
let result: i32 = test.to_string().split('%').collect()[0].parse().unwrap_or(0i32);
答案 0 :(得分:6)
# Message [/messages/{id}]
This resource represents one particular message identified by its *id*.
## Retrieve Message [GET]
+ Response 200 (application/json)
{"message":"Hello World!"}
+ Response 200 (text/plain)
Hello world!
+ Response 401 (text/plain)
There is no such a message for you, dear guest.
+ Response 410
The message you are searching for does not exist anymore.
fn main() {
let test = "5% of foo".to_string();
let result: i32 = test.split('%').collect::<Vec<_>>()[0].parse().unwrap_or(0);
}
可以成为实现collect()
的任何类型,因此需要提供类型提示。
或者,您可以通过使用延迟迭代器来提高效率。
FromIterator