Skip to content

Instantly share code, notes, and snippets.

@cohama
Last active April 23, 2017 03:58
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 cohama/2cad088c87b56dc82aec0c4ba5a5b0da to your computer and use it in GitHub Desktop.
Save cohama/2cad088c87b56dc82aec0c4ba5a5b0da to your computer and use it in GitHub Desktop.
fn main() {
println!("{:?}", take_while(&[1, 2, 3, 4, 5], |&x| x < 3));
}
fn take_while<T, F>(xs: &[T], f: F) -> &[T]
where F: Fn(&T) -> bool
{
let mut i = 0;
while f(&xs[i]) {
i += 1;
}
&xs[0..i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment