diff --git a/README.md b/README.md index fc17ce4..d02f3a3 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,5 @@ - https://tinchicus.com/2022/07/04/rust-while/ (while) - https://tinchicus.com/2022/07/05/rust-funciones-recursivas/ (recursiva) - https://tinchicus.com/2022/07/06/rust-punto-y-coma/ (ejemplo02) +- https://tinchicus.com/2022/07/12/rust-memoria-estatica/ (ejemplo03) +- https://tinchicus.com/2022/07/13/rust-struct/ (estructura) diff --git a/ejemplo03/Cargo.toml b/ejemplo03/Cargo.toml new file mode 100644 index 0000000..828aa73 --- /dev/null +++ b/ejemplo03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ejemplo03" +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/ejemplo03/src/main.rs b/ejemplo03/src/main.rs new file mode 100644 index 0000000..08faa3c --- /dev/null +++ b/ejemplo03/src/main.rs @@ -0,0 +1,16 @@ +fn main() { + let x = 5u32; + + let y: &u32; + { + let x_cuadrado = x * x; + let x_cubo = x_cuadrado * x; + // da un error por que la memoria es eliminada + // cuando es eliminado el scope + y = &(x_cubo + x_cuadrado + x); + }; + + let z = { 2 * x }; + + println!("x={}, y={}, z={}", z, y, z) +} diff --git a/estructura/Cargo.toml b/estructura/Cargo.toml new file mode 100644 index 0000000..40a2c7a --- /dev/null +++ b/estructura/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "estructura" +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/estructura/src/main.rs b/estructura/src/main.rs new file mode 100644 index 0000000..2eae1fb --- /dev/null +++ b/estructura/src/main.rs @@ -0,0 +1,65 @@ +struct Persona { + nombre: String, + edad: i32, + apellido: String, +} + +struct Area { + oficina: String, + puesto: String, +} + +struct Persona2 { + nombre: String, + edad: i32, + apellido: String, + area: Area, +} + +fn main() { + // usuario1 + let usuario = Persona { + nombre: String::from("Martín"), + apellido: String::from("Miranda"), + edad: 33, + }; + + println!( + "{} {}, {} años", + usuario.nombre, usuario.apellido, usuario.edad + ); + + // usuario2 + let mut usuario2 = Persona { + nombre: String::from("Federico"), + apellido: String::from("Caco"), + edad: 12, + }; + + // se puede cambiar por que usuario2 es mutable + usuario2.edad = 22; + + println!( + "{} {}, {} años", + usuario2.nombre, usuario2.apellido, usuario2.edad + ); + + // struct dentro de un struct + // usuario3 + let mut usuario3 = Persona2 { + nombre: String::from("Lucas"), + apellido: String::from("Pelotas"), + edad: 76, + area: Area { + oficina: String::from("IT"), + puesto: String::from("Tecnico"), + }, + }; + usuario3.edad = 45; + + println!("Nombre: {}", usuario3.nombre); + println!("Apellido: {}", usuario3.apellido); + println!("Edad: {}", usuario3.edad); + println!("Oficina: {}", usuario3.area.oficina); + println!("Puesto: {}", usuario3.area.puesto); +}