问题描述
我想替换内部 match
语句并为所有值工作,直到字母表用完为止.我知道我可以自己写,但我想使用内置函数.
I want to replace the inner match
statement and work for all values up to when the alphabet runs out. I know I can write it myself, but I want to use built-in functions.
fn convert(inp: u32, out: u32, numb: &String) -> Result<String, String> {
match isize::from_str_radix(numb, inp) {
Ok(a) => match out {
2 => Ok(format!("{:b}", a)),
8 => Ok(format!("{:o}", a)),
16 => Ok(format!("{:x}", a)),
10 => Ok(format!("{}", a)),
0 | 1 => Err(format!("No base lower than 2!")),
_ => Err(format!("printing in this base is not supported")),
},
Err(e) => Err(format!(
"Could not convert {} to a number in base {}.
{:?}
",
numb, inp, e
)),
}
}
推荐答案
如果你想获得更多的性能,你可以创建一个结构体并实现 Display
或 Debug代码>为它.这避免了分配
String
.为了最大限度地进行过度设计,您还可以使用堆栈分配的数组来代替 Vec
.
If you wanted to eke out a little more performance, you can create a struct and implement Display
or Debug
for it. This avoids allocating a String
. For maximum over-engineering, you can also have a stack-allocated array instead of the Vec
.
这是 Boiethios 的回答,应用了这些更改:
Here is Boiethios' answer with these changes applied:
struct Radix {
x: i32,
radix: u32,
}
impl Radix {
fn new(x: i32, radix: u32) -> Result<Self, &'static str> {
if radix < 2 || radix > 36 {
Err("Unnsupported radix")
} else {
Ok(Self { x, radix })
}
}
}
use std::fmt;
impl fmt::Display for Radix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut x = self.x;
// Good for binary formatting of `u128`s
let mut result = [' '; 128];
let mut used = 0;
let negative = x < 0;
if negative {
x*=-1;
}
let mut x = x as u32;
loop {
let m = x % self.radix;
x /= self.radix;
result[used] = std::char::from_digit(m, self.radix).unwrap();
used += 1;
if x == 0 {
break;
}
}
if negative {
write!(f, "-")?;
}
for c in result[..used].iter().rev() {
write!(f, "{}", c)?;
}
Ok(())
}
}
fn main() {
assert_eq!(Radix::new(1234, 10).to_string(), "1234");
assert_eq!(Radix::new(1000, 10).to_string(), "1000");
assert_eq!(Radix::new(0, 10).to_string(), "0");
}
这仍然可以通过以下方式进行优化:
This could still be optimized by:
- 创建一个 ASCII 数组而不是
char
数组 - 不对数组进行零初始化
由于这些途径需要 unsafe
或像 arraybuf 这样的外部 crate,我没有包括他们.您可以在 标准库的内部实现细节.
Since these avenues require unsafe
or an external crate like arraybuf, I have not included them. You can see sample code in internal implementation details of the standard library.
这篇关于是否有一个内置函数可以将数字转换为任何基数的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!