我正在尝试实现一个网卡游戏,我需要解析一个字符串并获得排名值为u32。字符串看起来像这样
let response: String = "(6, Hearts)".to_string();
我尝试过使用:
let rank = response.chars().nth(1).unwrap() as u32;
用这个我得到54而不是6的等级我认为因为它返回一个字节索引而不是实际值。我也试过这个:
let rank: u32;
response.chars()
.find(|x|
if x.is_digit(10) {
rank = x.to_digit(10).unwrap();
}
);
println!("rank {}", rank);
对于这个我在闭包上遇到不匹配类型错误
Compiling playground v0.0.1 (file:///playground)
error[E0308]: mismatched types
--> src/main.rs:12:35
|
12 | if x.is_digit(10) {
| ___________________________________^
13 | | rank = x.to_digit(10).unwrap();
14 | | }
| |_________________^ expected bool, found ()
|
= note: expected type `bool`
found type `()`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
以下是代码的链接:https://play.rust-lang.org/?gist=120b0ee308c335c9bc79713d3875a3b4&version=stable&mode=debug