gpt4 book ai didi

rust - 将 char 解析为 u32

转载 作者:行者123 更新时间:2023-11-29 07:47:16 25 4
gpt4 key购买 nike

如问题所述,我该如何实现?

如果我有这样的代码:

let a = "29";
for c in a.chars() {
println!("{}", c as u32);
}

我得到的是 2 和 9 的 unicode 代码点:

  • 50
  • 57

我想要的是将这些字符解析为实际数字。

最佳答案

char::to_digit(radix)这样做。 radix 表示“基数”,即十进制为 10,十六进制为 16,等等:

let a = "29";
for c in a.chars() {
println!("{:?}", c.to_digit(10));
}

它返回一个Option,所以你需要unwrap()它,或者更好:expect("that's no number!") .您可以在 the appropriate chapter of the Rust book 中阅读有关正确错误处理的更多信息.

关于rust - 将 char 解析为 u32,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com