Skip to content

Instantly share code, notes, and snippets.

@mykhailokrainik
Created January 9, 2019 21:52
Show Gist options
  • Save mykhailokrainik/68f867f2ccd340c074c712b8f65ac950 to your computer and use it in GitHub Desktop.
Save mykhailokrainik/68f867f2ccd340c074c712b8f65ac950 to your computer and use it in GitHub Desktop.
Rust Default::default()
use std::fmt;
struct ApiUrl<'a> {
host: &'a str,
port: &'a str,
path: &'a str,
}
impl Default for ApiUrl<'_> {
fn default() -> Self {
ApiUrl {
host: "http://example.com",
port: "8000",
path: "/",
}
}
}
impl fmt::Display for ApiUrl<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}{}", self.host, self.port, self.path)
}
}
fn main() {
let mut request: ApiUrl = Default::default();
request.path = "/login";
println!("URL {}", request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment