Created
January 9, 2019 21:52
-
-
Save mykhailokrainik/68f867f2ccd340c074c712b8f65ac950 to your computer and use it in GitHub Desktop.
Rust Default::default()
This file contains hidden or 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
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