diff options
| author | Carlos Maiolino <[email protected]> | 2025-09-06 09:30:14 +0200 |
|---|---|---|
| committer | Carlos Maiolino <[email protected]> | 2025-09-06 09:30:14 +0200 |
| commit | 93b1c04a218858ecc59b6b8929103695b7b8c2a0 (patch) | |
| tree | 7ae24ff4b2ef06c8d961f2c908ba8511e1fc995b /rust/chap4/slice_me.rs | |
| parent | 973e27b243ea7f12b6743894465c67a4a6a87eb2 (diff) | |
Move rust playground here
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'rust/chap4/slice_me.rs')
| -rw-r--r-- | rust/chap4/slice_me.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/rust/chap4/slice_me.rs b/rust/chap4/slice_me.rs new file mode 100644 index 0000000..da201a1 --- /dev/null +++ b/rust/chap4/slice_me.rs @@ -0,0 +1,26 @@ + +/* Return the index of the first space or s.len()*/ +fn get_word_index(s: &str) -> usize { + let bytes = s.as_bytes(); + + for (i, &item) in bytes.iter().enumerate() { + if item == b' ' { + return i; + } + } + + return s.len(); +} + +fn first_word(s: &str) -> &str { + let idx = get_word_index(s); + + return &s[..idx]; +} + +fn main() { + let s = String::from("corno manso ronaldo curintiano"); + let slice = first_word(&s); + + println!("{slice}"); +} |
