iteradores y _

This commit is contained in:
2023-06-06 13:44:20 +02:00
parent d9437e766d
commit 07d62f050e
5 changed files with 44 additions and 1 deletions

View File

@@ -18,3 +18,5 @@
- https://tinchicus.com/2022/06/23/rust-match/ (coincidir)
- https://tinchicus.com/2022/06/24/rust-funciones-y-metodos/ (ejemplo01)
- https://tinchicus.com/2022/06/27/rust-closures/ (cierre)
- https://tinchicus.com/2022/06/28/rust-iteradores/ (iter)
- https://tinchicus.com/2022/06/29/rust-el-parametro-_/ (bucles2)

8
bucles2/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "bucles2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

8
bucles2/src/main.rs Normal file
View File

@@ -0,0 +1,8 @@
fn main() {
let mi_arreglo = ["Lovecraft", "Poe", "Barker", "King"];
// Evita avisos en el compilador y sirve para almacenar datos que no son necesarios
for (_, apellido) in mi_arreglo.iter().enumerate() {
println!("{}", apellido);
}
}

8
iter/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "iter"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

17
iter/src/main.rs Normal file
View File

@@ -0,0 +1,17 @@
fn main() {
let v1 = vec![1, 2, 3];
let v1_iter = v1.iter();
for val in v1_iter {
println!("{}", val);
}
let v2 = vec![50, 100, 200];
let v2_iter = v2.iter();
v2_iter.for_each(|e| print!("{}, ", e));
let mut v3 = vec![50, 100, 200];
v3.iter_mut().for_each(|e| *e += 23);
println!("{:?}", v3)
}