entrada, argumento, bucles y condición

This commit is contained in:
2023-06-02 18:44:19 +02:00
parent c945ae68ec
commit bf9b2271d0
9 changed files with 105 additions and 1 deletions

View File

@@ -11,3 +11,7 @@ https://tinchicus.com/2022/09/26/rust-listado-del-curso-inicial/
- 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)
- 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)

8
argumento/Cargo.toml Normal file
View File

@@ -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]

12
argumento/src/main.rs Normal file
View File

@@ -0,0 +1,12 @@
use std::env;
// cargo run Huevos Pelotas Narices
fn main() {
let args: Vec<String> = env::args().collect();
println!(
"Pasaste {:?} argumentos, ellos fueron {:?}",
args.len() - 1,
&args[1..]
);
}

8
bucles/Cargo.toml Normal file
View File

@@ -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]

7
bucles/src/main.rs Normal file
View File

@@ -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);
}
}

8
condicion/Cargo.toml Normal file
View File

@@ -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]

19
condicion/src/main.rs Normal file
View File

@@ -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();
}
}

8
entrada/Cargo.toml Normal file
View File

@@ -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]

30
entrada/src/main.rs Normal file
View File

@@ -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<usize, io::Error> = 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<i32> = 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),
};
}