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