From 93b1c04a218858ecc59b6b8929103695b7b8c2a0 Mon Sep 17 00:00:00 2001 From: Carlos Maiolino Date: Sat, 6 Sep 2025 09:30:14 +0200 Subject: Move rust playground here Signed-off-by: Carlos Maiolino --- rust/chap4/slice_me.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 rust/chap4/slice_me.rs (limited to 'rust/chap4/slice_me.rs') 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}"); +} -- cgit v1.2.3