From 78e5f6f969085d9422764ea2ab5cab8af8549d87 Mon Sep 17 00:00:00 2001 From: Manuel Riquelme Date: Tue, 30 May 2023 13:20:33 +0200 Subject: [PATCH] mapeo --- README.md | 3 +- mapeo/Cargo.toml | 8 +++++ mapeo/src/main.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 mapeo/Cargo.toml create mode 100644 mapeo/src/main.rs diff --git a/README.md b/README.md index 282c081..03fe75b 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,5 @@ https://tinchicus.com/2022/09/26/rust-listado-del-curso-inicial/ - https://tinchicus.com/2022/06/03/rust-strings/ (cadenas) - 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) \ No newline at end of file +- 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 diff --git a/mapeo/Cargo.toml b/mapeo/Cargo.toml new file mode 100644 index 0000000..7de16a8 --- /dev/null +++ b/mapeo/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "mapeo" +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/mapeo/src/main.rs b/mapeo/src/main.rs new file mode 100644 index 0000000..0c36cc9 --- /dev/null +++ b/mapeo/src/main.rs @@ -0,0 +1,77 @@ +use std::collections::HashMap; + +fn main() { + println!("-- Ejercicio 1"); + let mut mapa = HashMap::new(); + + mapa.insert('A', 10); + mapa.insert('B', 20); + + let idx = 'A'; + let mapeo = mapa.get(&idx); + println!("{:?}", mapeo); + + println!("-- Ejercicio 2"); + + let mut mapa = HashMap::new(); + + mapa.insert(String::from("primero"), 10); + mapa.insert(String::from("segundo"), 20); + + let idx = String::from("primero"); + let mapeo = mapa.get(&idx); + println!("{:?}", mapeo); + + println!("-- Ejercicio 3"); + + let mut mapa = HashMap::new(); + + mapa.insert(String::from("primero"), 10); + mapa.insert(String::from("segundo"), 20); + + for (clave, valor) in &mapa { + println!("{}: {}", clave, valor); + } + + println!("-- Ejercicio 4"); + + let mut mapa = HashMap::new(); + + mapa.insert(String::from("primero"), 10); + mapa.insert(String::from("segundo"), 20); + + mapa.insert(String::from("primero"), 30); + + for (clave, valor) in &mapa { + println!("{}: {}", clave, valor); + } + + println!("-- Ejercicio 5"); + + let mut mapa = HashMap::new(); + + mapa.insert(String::from("primero"), 10); + mapa.insert(String::from("segundo"), 20); + + mapa.entry(String::from("primero")).or_insert(30); + mapa.entry(String::from("tercero")).or_insert(40); + + for (clave, valor) in &mapa { + println!("{}: {}", clave, valor); + } + + println!("-- Ejercicio 6"); + + let texto = "hola mundo maravilloso mundo"; + + let mut mapa = HashMap::new(); + + for palabra in texto.split_whitespace() { + let contar = mapa.entry(palabra).or_insert(0); + *contar += 1; + } + + for (clave, valor) in &mapa { + println!("{}: {}", clave, valor); + } +}