some more stuff
This commit is contained in:
parent
cc2f1b67a8
commit
eb9773627e
12 changed files with 150 additions and 0 deletions
7
ch4/slices/Cargo.lock
generated
Normal file
7
ch4/slices/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "slices"
|
||||
version = "0.1.0"
|
||||
6
ch4/slices/Cargo.toml
Normal file
6
ch4/slices/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "slices"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
18
ch4/slices/src/main.rs
Normal file
18
ch4/slices/src/main.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
fn first_word(s: &String) -> usize {
|
||||
let bytes = s.as_bytes();
|
||||
|
||||
for (i, &item) in bytes.iter().enumerate() {
|
||||
if item == b' ' {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
s.len()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut s = String::from("hello world");
|
||||
let word = first_word(&s);
|
||||
println!("{}", word);
|
||||
s.clear();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue