From 643fda700c3e99e17a21830788ba2ddda4ab0222 Mon Sep 17 00:00:00 2001 From: Manuel Riquelme Date: Fri, 23 Jun 2023 17:00:46 +0200 Subject: [PATCH] enums --- README.md | 1 + enums/Cargo.toml | 8 ++++++++ enums/src/main.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 enums/Cargo.toml create mode 100644 enums/src/main.rs diff --git a/README.md b/README.md index 00cd65c..4622a2d 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,4 @@ - https://tinchicus.com/2022/07/13/rust-struct/ (estructura) - https://tinchicus.com/2022/07/14/rust-struct-en-multiples-archivos/ (estructura2) - https://tinchicus.com/2022/07/15/rust-tuple-struct/ (nuevotipo) +- https://tinchicus.com/2022/07/18/rust-enum/ (enums) diff --git a/enums/Cargo.toml b/enums/Cargo.toml new file mode 100644 index 0000000..7b703c4 --- /dev/null +++ b/enums/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "enums" +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/enums/src/main.rs b/enums/src/main.rs new file mode 100644 index 0000000..8b361ed --- /dev/null +++ b/enums/src/main.rs @@ -0,0 +1,32 @@ +enum Numerado1 { + TipoTuple(f32, i32, String), + TipoStruct { var1: i32, var2: f32 }, + StructTuple(i32), + Variable, +} + +enum Numerado2 { + TipoTuple(f32, i32, String), + TipoStruct { var1: i32, var2: f32 }, + StructTuple(i32), +} + +fn main() { + let mut texto1 = "".to_owned(); + let mut texto2 = "".to_owned(); + let mut num1 = 0f32; + + let valor = Numerado1::TipoTuple(3.14, 1, "Hola".to_owned()); + let valor2 = Numerado2::TipoTuple(3.14, 0, "Mundo".to_owned()); + + if let Numerado1::TipoTuple(f, i, s) = valor { + texto1 = s; + num1 = f; + } + + if let Numerado2::TipoTuple(f, i, s) = valor2 { + texto2 = s; + } + + println!("{}, {}! del hombre {}", texto1, texto2, num1) +}