Skip to content

Instantly share code, notes, and snippets.

@krisselden
Created September 18, 2019 05:15
Show Gist options
  • Save krisselden/1b0f8a25d61b035dbbf636443d5a8784 to your computer and use it in GitHub Desktop.
Save krisselden/1b0f8a25d61b035dbbf636443d5a8784 to your computer and use it in GitHub Desktop.
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