Created
June 12, 2026 06:34
-
-
Save gusdelact/b34b63807c7f95212f106e6a7b7d65df to your computer and use it in GitHub Desktop.
ejemplo04_meetup_junio11.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[derive (Debug)] | |
| struct Mia { | |
| campo1: u8, | |
| campo2: f32 | |
| } | |
| fn f01(par01: u8) -> () { | |
| let a01= par01 +1; | |
| println!("{:p}",&par01); | |
| } | |
| fn f02(par02:Mia) -> (){ | |
| println!("{:?}",par02); | |
| println!("{:p}",&par02); | |
| } | |
| fn f03(par03:&Mia) -> (){ | |
| println!("{:?}",par03); | |
| println!("{:p}",&par03); | |
| } | |
| fn f04(par04:&mut Mia) -> (){ | |
| println!("{:?}",par04); | |
| println!("{:p}",&par04); | |
| par04.campo1 = 42; | |
| par04.campo2 = 2.718; | |
| } | |
| fn main() { | |
| let x: u8; | |
| x = 5; | |
| println!("{}",x); | |
| println!("{:p}",&x); | |
| { //bloque de codigo | |
| let y: u8 = 0x10; | |
| println!("{:p}",&y); | |
| let z = x + y; | |
| } | |
| // x = 6; no se puede cambiar | |
| let mut u ; //esta si puede cambiar | |
| u =5; | |
| println!("{}",u); | |
| u = x + 3; | |
| println!("{}",u); | |
| //usar struct Mia | |
| let m1 = Mia{campo1:0x32,campo2:3.14159 }; //inmutable | |
| println!("{:?}",m1); | |
| println!("{:p}",&m1); | |
| //invocar funciones | |
| f01(x); | |
| println!("{}",x); | |
| f01(u); | |
| println!("{}",u); | |
| //invocar funcion que recibe como parametro un struct | |
| //f02(m1); //se presta a m1 | |
| //println!("{:?}",m1); //pero yo ya no la puedo utilizar a m1 :() | |
| f03(&m1); //estoy prestando m1 pero ... | |
| println!("{:?}",m1); //cuando regresa de f03 es mia de nuevo | |
| //mutabilidad y ownership y borrowing | |
| let mut m2 = Mia{campo1:0x10,campo2:0.7071 }; | |
| f04(&mut m2); | |
| println!("{:?}",m2); | |
| //un ejemplo con bloque de codigo | |
| let a0={ | |
| let b0:f32 = -4.44; | |
| m2.campo1 = 0x9; | |
| m2.campo2 = -0.56; | |
| let c0 =( m2.campo1 as f32) * m2.campo2 + b0; | |
| c0 | |
| }; | |
| println!("{:?}",a0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment