Created
September 18, 2019 05:15
-
-
Save krisselden/1b0f8a25d61b035dbbf636443d5a8784 to your computer and use it in GitHub Desktop.
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
struct Path<'a> { | |
path: &'a str, | |
} | |
impl<'a> Path<'a> { | |
fn new(path: &str) -> Path { | |
Path { path } | |
} | |
fn query(&self) -> &'a str { | |
if let Some(i) = self.path.find('?') { | |
&self.path[i..] | |
} else { | |
self.path | |
} | |
} | |
} | |
fn query(path: &str) -> &str { | |
// we have to explicitly tie the lifetime of the str passed into Path | |
// with the slice that is returned or it will be coerced into | |
// the lifetime of the borrowed self which doesn't live past this | |
// function call | |
Path::new(path).query() | |
} | |
fn main() { | |
println!("{}", query("http://example.com/path?query=1")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment