rust_the_book/string_slicing/src/main.rs
2020-05-22 13:25:47 +02:00

18 lines
321 B
Rust

fn first_word(s: &String) -> &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));
}