Iterator 如何从Rust中的字符串解析int值

Iterator 如何从Rust中的字符串解析int值,iterator,rust,closures,numeric,string-parsing,Iterator,Rust,Closures,Numeric,String Parsing,我正在尝试实现一个网卡游戏,我需要解析一个字符串,并将排名值作为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|

我正在尝试实现一个网卡游戏,我需要解析一个字符串,并将排名值作为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.

下面是代码的链接:

let rank=response.chars().flat_map(|x | x.to_digital(10)).next().unwrap()
让rank=response.chars().nth(1).unwrap()作为u32-'0'作为u32
让rank:u32=响应[1..][1].parse().unwrap(),等等。我鼓励您重新阅读以更新自己的基本数据类型。在Rust中,单个字符和字符串是相关但不同的数据类型。