Skip to content

Instantly share code, notes, and snippets.

@Gonzih
Last active February 26, 2020 18:05
Show Gist options
  • Save Gonzih/ee8feb1b2cf4cd01d43ac31f12ae26c9 to your computer and use it in GitHub Desktop.
Save Gonzih/ee8feb1b2cf4cd01d43ac31f12ae26c9 to your computer and use it in GitHub Desktop.
Optional arguments in Rust
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