Is there anything in Rust to convert a binary string to an integer?
First of all, you should be using the official docs; those you pointed to are way outdated.
You have a string and you can't format a string as an integer. I think what you want is a parser. Here's a version using from_str_radix
:
fn main() {
let bin_idx = "01110011001";
let intval = isize::from_str_radix(bin_idx, 2).unwrap();
println!("{}", intval);
}
(playground)
As an alternative you can use:
let bstr = "11010101";
let i = i32::from_str_radix(bstr, 2).expect("Not a binary number!");
println!("{i}");
Which should print 213
.
EDIT: Posted that answer as I thought isize
for some reason was not available in my embedded rust project. Was just a typo though...