just a little cleanup

This commit is contained in:
Marc Planard 2022-11-23 13:53:40 +01:00
parent 921497ab45
commit af2a5eff0a

View File

@ -1,39 +1,36 @@
use std::env; use std::env;
/* Not used, example of an enum
#[derive(Debug)] #[derive(Debug)]
enum Maybe<T> { enum Maybe<T> {
Just(T), Just(T),
Nothing Nothing
} }
*/
fn main() { fn main() {
let arg = env::args().nth(1); let arg = env::args().nth(1);
println!("{:?}", Maybe::Just(42)); // println!("{:?}", Maybe::Just(42));
match arg { match arg {
Some(result) => { Some(result) => {
let ip = ip_to_int(&result); let ip = ip_to_int(&result);
println!("{:b}", ip); println!("{:b}", ip);
},
None => println!("need one arg")
} }
None => println!("need one arg"),
}
} }
fn ip_to_int(ip_str: &str) -> u32 { fn ip_to_int(ip_str: &str) -> u32 {
let mut tbl : Vec<&str> = ip_str.split(".") let mut tbl: Vec<&str> = ip_str.split('.').collect();
.collect();
tbl.reverse(); tbl.reverse();
let mut ip = 0_u32; let mut ip = 0_u32;
for (idx, s) in tbl.iter().enumerate() { for (idx, s) in tbl.iter().enumerate() {
let nbr : u8 = s.parse() let nbr: u8 = s.parse().expect("parse fail");
.expect("parse fail"); ip += (nbr as u32) << (8 * idx);
ip += (nbr as u32) << 8*idx;
} }
ip ip
} }