Advertisement
nhaplycafedang

Untitled

Feb 8th, 2022
1,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.35 KB | None | 0 0
  1. fn main() {
  2.     let mut s = String::from("Hello world!");
  3.     let word = first_word(&s);
  4.     s.clear(); // ok
  5.     println!("{} : {}", s, word)
  6. }
  7.  
  8. fn first_word(s: &String) -> usize {
  9.     let bytes = s.as_bytes();
  10.     for (i, &item) in bytes.iter().enumerate() {
  11.         if item == b' ' {
  12.             return i;
  13.         }
  14.     }
  15.  
  16.     s.len()
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement