This commit is contained in:
alban 2022-11-23 10:49:11 +01:00
commit 921497ab45
4 changed files with 54 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

5
Cargo.lock generated Normal file
View File

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "tuto"
version = "0.1.0"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "tuto"
version = "0.1.0"
authors = ["alban <git@albancrommer.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

39
src/main.rs Normal file
View File

@ -0,0 +1,39 @@
use std::env;
#[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")
}
}
fn ip_to_int(ip_str: &str) -> u32 {
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
}