// Created on savesnippets.com ยท https://savesnippets.com/JqdyvFeLMZrvN8 #[derive(Debug)] struct Request { url: String, method: String, headers: Vec<(String, String)>, body: Option, timeout: u32, } #[derive(Default)] struct RequestBuilder { url: Option, method: String, headers: Vec<(String, String)>, body: Option, timeout: u32, } impl RequestBuilder { fn url(mut self, url: impl Into) -> Self { self.url = Some(url.into()); self } fn method(mut self, m: &str) -> Self { self.method = m.into(); self } fn header(mut self, k: &str, v: &str) -> Self { self.headers.push((k.into(), v.into())); self } fn body(mut self, b: impl Into) -> Self { self.body = Some(b.into()); self } fn timeout(mut self, secs: u32) -> Self { self.timeout = secs; self } fn build(self) -> Result { Ok(Request { url: self.url.ok_or("url is required")?, method: if self.method.is_empty() { "GET".into() } else { self.method }, headers: self.headers, body: self.body, timeout: if self.timeout == 0 { 30 } else { self.timeout }, }) } } fn main() -> Result<(), &'static str> { let req = RequestBuilder::default() .url("https://api.example.com/users") .method("POST") .header("Content-Type", "application/json") .body(r#"{"name":"Alice"}"#) .timeout(10) .build()?; println!("{req:#?}"); Ok(()) }