Last active
May 2, 2016 12:06
-
-
Save bugaevc/1a526847fedc37ef1af83202cf066a1e to your computer and use it in GitHub Desktop.
This file contains 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
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