Hostname named struct

This commit is contained in:
Milo Turner 2020-03-07 13:29:33 -05:00
parent 7107b88a4f
commit 494ad166a3
1 changed files with 11 additions and 5 deletions

View File

@ -1,10 +1,13 @@
extern crate hptp; extern crate hptp;
#[derive(Debug)] #[derive(Debug)]
struct Hostname(String, u16); struct Hostname {
ip: String,
port: u16,
}
fn go(hn: Hostname) { fn go(hn: Hostname) {
println!("{:?}", hn) println!("ip={}, port={}", hn.ip, hn.port)
} }
fn usage() { fn usage() {
@ -15,9 +18,12 @@ fn parse_args() -> Result<Hostname, ()> {
let mut args = std::env::args(); let mut args = std::env::args();
let arg = args.nth(1).ok_or(())?; let arg = args.nth(1).ok_or(())?;
let i = arg.find(':').ok_or(())?; let i = arg.find(':').ok_or(())?;
let ip: String = arg[..i].into(); let ip = &arg[..i];
let port: u16 = arg[i + 1..].parse::<u16>().map_err(|_| ())?; let port: u16 = arg[i + 1..].parse().map_err(|_| ())?;
Ok(Hostname(ip, port)) Ok(Hostname {
ip: ip.into(),
port,
})
} }
fn main() { fn main() {