rust_the_book/string_slicing/src/main.rs
laurens 6be18e3e3e string slicing: enhance API to accept string slices
This way, the API is more general and we can accept any kind of string
2020-05-22 13:28:50 +02:00

18 lines
318 B
Rust

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[..]
}
fn main() {
let s = String::from("Hello world");
println!("String: {}", s);
println!("First word: {}", first_word(&s));
}