Last active
February 26, 2020 18:05
-
-
Save Gonzih/ee8feb1b2cf4cd01d43ac31f12ae26c9 to your computer and use it in GitHub Desktop.
Optional arguments in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Links = Vec<String>; | |
type Proxies = Vec<String>; | |
#[derive(Clone, Debug)] | |
struct Opts { | |
links: Option<Links>, | |
proxies: Option<Proxies>, | |
} | |
impl Opts { | |
fn new() -> Self { | |
Opts { | |
links: None, | |
proxies: None, | |
} | |
} | |
fn with_links(self, input: Vec<&str>) -> Self { | |
let mut new = self; | |
new.links = Some(input.iter().map(|s| s.to_string()).collect()); | |
new | |
} | |
fn with_proxies(self, input: Vec<&str>) -> Self { | |
let mut new = self; | |
new.proxies = Some(input.iter().map(|s| s.to_string()).collect()); | |
new | |
} | |
} | |
fn with_opts(_: &str, args: Opts) { | |
println!("{:#?}", args); | |
} | |
fn main() { | |
with_opts("hello", Opts::new().with_links(vec!["http://blabla.com"])); | |
with_opts("hello", Opts::new().with_proxies(vec!["localhost:8080"])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment