Last active
October 12, 2019 23:45
-
-
Save gusdelact/094fe907a9282de85b1509907f645287 to your computer and use it in GitHub Desktop.
This file contains 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
use std::fmt; | |
#[derive(Debug)] | |
struct Dato { | |
campo : u32 | |
} | |
#[derive(Debug)] | |
struct Estructura { | |
cadena : String, | |
entero : i32, | |
real : f64, | |
} | |
#[derive(Debug)] | |
struct Estructura2<'a> { | |
cadena : &'a str, | |
entero : i32, | |
real : f64, | |
} | |
impl fmt::Display for Estructura { | |
fn fmt(&self, f: &mut fmt::Formatter ) -> fmt::Result { | |
write!(f, "( {} -- {} -- {} )", self.cadena, self.entero, self.real) | |
} | |
} | |
#[derive(Debug)] | |
struct Lista(Vec<i32>) ; | |
impl fmt::Display for Lista { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
let vec = &self.0; | |
write!(f,"[ "); | |
for (count,data) in vec.iter().enumerate() { | |
write!(f,"( count:{} , data: {} ) ", count ,data)?; | |
} | |
write!(f," ]") | |
} | |
} | |
fn main() { | |
let mut vMain = 2; | |
f01(vMain); | |
vMain += 1; | |
f02(vMain); | |
vMain += 1; | |
f03(vMain); | |
let mut dato: Dato = Dato { campo : 1}; | |
f03(dato.campo); | |
f05(&dato); | |
dato.campo = dato.campo + 1; | |
f06(&dato); | |
dato.campo = dato.campo + 1; | |
f07(&mut dato); | |
dato.campo = dato.campo + 1; | |
f05(&dato); | |
let copia = f08(&dato); | |
f05(&copia); | |
let pi = 3.141592; | |
let pi4 = format!("{:.*}", 4, pi); | |
println!("{}",pi4); | |
let estr01 = Estructura {cadena: "hola".to_string() , entero: 4, real: 3.14159}; | |
println!("{:p}",&estr01); | |
f10(&estr01); | |
let estr02 = Estructura2 {cadena: &"hola".to_string() , entero: 4, real: 3.14159}; | |
println!("{:p}",&estr02); | |
f11(&estr02); | |
let lista00 = Lista(vec![1,2,3,4,5]); | |
println!("{}",lista00); | |
f12(&lista00); | |
let heap_entero = Box::new(100u32); | |
println!("{:p}", heap_entero); | |
f01(*heap_entero); | |
} | |
fn f01(par01: u32) { | |
println!("{} {:p}", par01, &par01); | |
} | |
fn f02(par01: u32) { | |
f01( par01); | |
} | |
fn f03(par01: u32) { | |
f02( par01); | |
} | |
fn f04(mut par01: u32) { | |
let mut x = 0; | |
par01 = par01 + x; | |
f01(par01); | |
x= par01 + 2; | |
f01(x); | |
} | |
fn f05(par01: &Dato) { | |
println!("{:?} {:p}", par01, par01); | |
} | |
fn f06(par01: &Dato) { | |
f05(par01); | |
} | |
fn f07(par01: &mut Dato) { | |
par01.campo = par01.campo + 2; | |
f05(par01); | |
} | |
fn f08(par01: &Dato) -> Dato { | |
let mut resultado= Dato { campo : 0 }; | |
resultado.campo = par01.campo + 5; | |
resultado | |
} | |
fn f09(cadena: String) -> String { | |
println!("{}",cadena); | |
"gracias".to_string() | |
} | |
fn f10(par01 : &Estructura) { | |
println!("{} {:p}",par01,par01); | |
} | |
fn f11(par01 : &Estructura2) { | |
println!("{:#?} {:p}",par01,par01); | |
} | |
fn f12(par01 : &Lista) { | |
println!("{} {:p}",par01,par01); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment