trpl-zh-cn/listings/ch04-understanding-ownership/no-listing-18-first-word-slice/src/main.rs
2022-02-06 16:43:51 +08:00

16 lines
251 B
Rust
Executable File

// ANCHOR: here
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
// ANCHOR_END: here
fn main() {}