I'm using the following code to convert a string representation of an octal into a u32
:
pub fn umask() -> String {
match env::var("UMASK") {
Ok(res) => res.into(),
_ => "777".into()
}
}
fn main() {
println!("{:#?}", u32::from_str_radix(format!("0o{}", umask()).as_str(), 8));
}
Instead all I Get is:
Err(
ParseIntError {
kind: InvalidDigit,
},
)
According to this it looks like it's in the correct format and as it's an octal, it's base 8 thus a radix of 8 is what you need.
What am I doing wrong?
Drop the 0o
prefix. from_str_radix
does not expect any kind of a prefix, as the radix is already given as a separate argument.
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("{}", u32::from_str_radix("777", 8)?); // Prints: 511
println!("{}", u32::from_str_radix("0777", 8)?); // Prints: 511
Ok(())
}