Skip to content

Instantly share code, notes, and snippets.

@bugaevc
Last active May 2, 2016 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bugaevc/1a526847fedc37ef1af83202cf066a1e to your computer and use it in GitHub Desktop.
Save bugaevc/1a526847fedc37ef1af83202cf066a1e to your computer and use it in GitHub Desktop.
fn search<'a, 'b>(needle: &'a str, haystack: &'b str) -> Option<&'b str> {
// imagine some clever algorithm here
// that returns a slice of the original string
let len = needle.len();
if haystack.chars().nth(0) == needle.chars().nth(0) {
Some(&haystack[..len])
} else if haystack.chars().nth(1) == needle.chars().nth(0) {
Some(&haystack[1..len+1])
} else {
None
}
}
fn main() {
let haystack = "hello little girl";
let res;
{
let needle = String::from("ello");
res = search(&needle, haystack);
}
match res {
Some(x) => println!("found {}", x),
None => println!("nothing found")
}
// outputs "found ello"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment