memorias y struct
This commit is contained in:
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