mapeo
This commit is contained in:
@@ -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)
|
||||
- https://tinchicus.com/2022/06/08/rust-tuple/ (tupla)
|
||||
- https://tinchicus.com/2022/06/09/rust-hash-map/ (mapeo)
|
||||
8
mapeo/Cargo.toml
Normal file
8
mapeo/Cargo.toml
Normal file
@@ -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]
|
||||
77
mapeo/src/main.rs
Normal file
77
mapeo/src/main.rs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user