Skip to content

Instantly share code, notes, and snippets.

@ameernormie
Created March 13, 2020 18:40
Show Gist options
  • Save ameernormie/807d8f300e8826fb7d21fe5e290a9f70 to your computer and use it in GitHub Desktop.
Save ameernormie/807d8f300e8826fb7d21fe5e290a9f70 to your computer and use it in GitHub Desktop.
How to use slices in rust
fn main(){
let s = String::from("hello world");
let x = first_word(&s);
println!("first word is {}", x);
}
fn first_word(s: &str) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[..i];
}
}
&s[..]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment