memorias y struct
This commit is contained in:
@@ -25,3 +25,5 @@
|
|||||||
- https://tinchicus.com/2022/07/04/rust-while/ (while)
|
- https://tinchicus.com/2022/07/04/rust-while/ (while)
|
||||||
- https://tinchicus.com/2022/07/05/rust-funciones-recursivas/ (recursiva)
|
- 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/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)
|
||||||
|
|||||||
8
ejemplo03/Cargo.toml
Normal file
8
ejemplo03/Cargo.toml
Normal file
@@ -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]
|
||||||
16
ejemplo03/src/main.rs
Normal file
16
ejemplo03/src/main.rs
Normal file
@@ -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)
|
||||||
|
}
|
||||||
8
estructura/Cargo.toml
Normal file
8
estructura/Cargo.toml
Normal file
@@ -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]
|
||||||
65
estructura/src/main.rs
Normal file
65
estructura/src/main.rs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user