Skip to content

Instantly share code, notes, and snippets.

@orez-
Created June 15, 2021 03:06
Show Gist options
  • Save orez-/11a176393d4baa695706c7a6b41e5c39 to your computer and use it in GitHub Desktop.
Save orez-/11a176393d4baa695706c7a6b41e5c39 to your computer and use it in GitHub Desktop.
fn partition<'a>(s: &'a str, p: &str) -> (&'a str, &'a str) {
match s.find(p) {
Some(i) => (&s[..i], &s[i+p.len()..]),
None => (s, ""),
}
}
fn rpartition<'a>(s: &'a str, p: &str) -> (&'a str, &'a str) {
match s.rfind(p) {
Some(i) => (&s[..i], &s[i+p.len()..]),
None => ("", s),
}
}
fn main() {
println!("{:?}", partition("wowie zowie kablowie", " "));
println!("{:?}", partition("single", " "));
println!("{:?}", rpartition("wowie zowie kablowie", " "));
println!("{:?}", rpartition("single", " "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment