From bf9b2271d093cf55b503e3cfc537c7a8284b5106 Mon Sep 17 00:00:00 2001 From: clonbg Date: Fri, 2 Jun 2023 18:44:19 +0200 Subject: [PATCH] =?UTF-8?q?entrada,=20argumento,=20bucles=20y=20condici?= =?UTF-8?q?=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++++- argumento/Cargo.toml | 8 ++++++++ argumento/src/main.rs | 12 ++++++++++++ bucles/Cargo.toml | 8 ++++++++ bucles/src/main.rs | 7 +++++++ condicion/Cargo.toml | 8 ++++++++ condicion/src/main.rs | 19 +++++++++++++++++++ entrada/Cargo.toml | 8 ++++++++ entrada/src/main.rs | 30 ++++++++++++++++++++++++++++++ 9 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 argumento/Cargo.toml create mode 100644 argumento/src/main.rs create mode 100644 bucles/Cargo.toml create mode 100644 bucles/src/main.rs create mode 100644 condicion/Cargo.toml create mode 100644 condicion/src/main.rs create mode 100644 entrada/Cargo.toml create mode 100644 entrada/src/main.rs diff --git a/README.md b/README.md index b2abb6e..692f415 100644 --- a/README.md +++ b/README.md @@ -10,4 +10,8 @@ https://tinchicus.com/2022/09/26/rust-listado-del-curso-inicial/ - https://tinchicus.com/2022/06/13/rust-pasando-valores/ (porvalor y porreferencia) - https://tinchicus.com/2022/06/14/rust-la-libreria-std/ - https://tinchicus.com/2022/06/15/rust-println/ (println) -- https://tinchicus.com/2022/06/16/rust-format/ (format) \ No newline at end of file +- https://tinchicus.com/2022/06/16/rust-format/ (format) +- https://tinchicus.com/2022/06/17/rust-read_line/ (entrada) +- https://tinchicus.com/2022/06/20/rust-argumentos/ (argumento) +- https://tinchicus.com/2022/06/21/rust-for/ (bucles) +- https://tinchicus.com/2022/06/22/rust-if/ (condicion) \ No newline at end of file diff --git a/argumento/Cargo.toml b/argumento/Cargo.toml new file mode 100644 index 0000000..0b7635d --- /dev/null +++ b/argumento/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "argumento" +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/argumento/src/main.rs b/argumento/src/main.rs new file mode 100644 index 0000000..1d2709d --- /dev/null +++ b/argumento/src/main.rs @@ -0,0 +1,12 @@ +use std::env; + +// cargo run Huevos Pelotas Narices +fn main() { + let args: Vec = env::args().collect(); + + println!( + "Pasaste {:?} argumentos, ellos fueron {:?}", + args.len() - 1, + &args[1..] + ); +} diff --git a/bucles/Cargo.toml b/bucles/Cargo.toml new file mode 100644 index 0000000..a075ab7 --- /dev/null +++ b/bucles/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "bucles" +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/bucles/src/main.rs b/bucles/src/main.rs new file mode 100644 index 0000000..5b376fd --- /dev/null +++ b/bucles/src/main.rs @@ -0,0 +1,7 @@ +fn main() { + let mi_arreglo = ["Lovecraft", "Poe", "Barker", "King"]; + + for (pos, apellido) in mi_arreglo.iter().enumerate() { + println!("{} esta en la posicion {}", apellido, pos); + } +} diff --git a/condicion/Cargo.toml b/condicion/Cargo.toml new file mode 100644 index 0000000..5752a7b --- /dev/null +++ b/condicion/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "condicion" +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/condicion/src/main.rs b/condicion/src/main.rs new file mode 100644 index 0000000..fa219c0 --- /dev/null +++ b/condicion/src/main.rs @@ -0,0 +1,19 @@ +use std::io; + +fn main() { + let mut entrada: String = String::new(); + + for _ in 0..3 { + io::stdin().read_line(&mut entrada).unwrap(); + let n: i32 = entrada.trim().parse().unwrap(); + + if n < 0 { + println!("{} es negativo", n); + } else if n > 0 { + println!("{} es positivo", n); + } else { + println!("Es cero"); + } + entrada.clear(); + } +} diff --git a/entrada/Cargo.toml b/entrada/Cargo.toml new file mode 100644 index 0000000..901c52e --- /dev/null +++ b/entrada/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "entrada" +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/entrada/src/main.rs b/entrada/src/main.rs new file mode 100644 index 0000000..eefc0e7 --- /dev/null +++ b/entrada/src/main.rs @@ -0,0 +1,30 @@ +use std::io; + +fn main() { + let lector: io::Stdin = io::stdin(); + let mut entrada: String = String::new(); + + // metodo 1 de manejar los errores + lector.read_line(&mut entrada).expect("Fallo lectura"); + println!("Leido {}", entrada); + + // metodo 2 de manejar los errores + let resultado: Result = lector.read_line(&mut entrada); + + if resultado.is_err() { + println!("Fallo el ingreso de datos"); + return; + } + + println!("Leido {}", entrada); + + // metodo 3 de manejar los errores + let trimmed = entrada.trim(); + let opcion: Option = trimmed.parse().ok(); + + match opcion { + // match equivale a switch de otros lenguajes + Some(i) => println!("Tu entero de la entrada: {}", i), + None => println!("Este no es un entero: {}", trimmed), + }; +}