2

For instance:

let n = "1010";

let m: u32 = ?

It can be done by adding each digit multiplied by the right power of two but is there another, simpler, built-in way ?

1 Answer 1

7

You are looking for from_str_radix, available to all core integer types.

let n = "1010";
let m = u32::from_str_radix(n, 2)?;
assert_eq!(m, 10);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.