As a beginner in Rust i am trying to practice Rust with some little programs so here is my implementation of Decimal to Binary.
use std::io;
pub fn run() {
let number = input("Enter an input");
println!("{}", to_binary(number));
}
fn input(msg: &str) -> i32 {
println!("{}", msg);
let mut number = String::new();
io::stdin()
.read_line(&mut number)
.expect("Failed to read input");
match number.trim().parse() {
Ok(num) => num,
Err(_e) => panic!("Not a number"),
}
}
fn to_binary(mut decimal: i32) -> i32 {
if decimal == 0 {
decimal
} else {
let mut bits = String::new();
while decimal > 0 {
if decimal % 2 == 0 {
bits.push_str("0");
} else {
bits.push_str("1");
}
decimal /= 2;
}
// reverse the bits
match bits.chars().rev().collect::<String>().parse() {
Ok(num) => num,
Err(_e) => panic!("Something went wrong"),
}
}
}
so feel free to advice anything you consider. i just have three questions in particular
isn't it better to write
inputas a macro ?how can i write
to_binaryas pure function ?wouldn't be better to return binary type from
to_binaryinstead i32