From e61cdcb662550a75024e7b4df451d3ba3b49df59 Mon Sep 17 00:00:00 2001 From: Manuel Riquelme Date: Thu, 1 Jun 2023 11:41:43 +0200 Subject: [PATCH] slices --- README.md | 3 ++- slices/Cargo.toml | 8 ++++++++ slices/src/main.rs | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 slices/Cargo.toml create mode 100644 slices/src/main.rs diff --git a/README.md b/README.md index 03fe75b..9086a0e 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,5 @@ https://tinchicus.com/2022/09/26/rust-listado-del-curso-inicial/ - https://tinchicus.com/2022/06/06/rust-arrays/ (arreglos) - https://tinchicus.com/2022/06/07/rust-vector/ (vectores) - https://tinchicus.com/2022/06/08/rust-tuple/ (tupla) -- https://tinchicus.com/2022/06/09/rust-hash-map/ (mapeo) \ No newline at end of file +- https://tinchicus.com/2022/06/09/rust-hash-map/ (mapeo) +- https://tinchicus.com/2022/06/10/rust-slice/ (slices) \ No newline at end of file diff --git a/slices/Cargo.toml b/slices/Cargo.toml new file mode 100644 index 0000000..ffd8810 --- /dev/null +++ b/slices/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "slices" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/slices/src/main.rs b/slices/src/main.rs new file mode 100644 index 0000000..5160179 --- /dev/null +++ b/slices/src/main.rs @@ -0,0 +1,16 @@ +fn primera_palabra(s: &String) -> usize { + let b=s.as_bytes(); + for (i,&item) in b.iter().enumerate() { + if item == b' ' { + return i + } + } + s.len() +} + +fn main() { + let s = String::from("Hola Mundo Cruel"); + let pos=primera_palabra(&s); + let palabra=&s[0..pos]; + println!("{}",palabra) +} \ No newline at end of file