SHARE
TWEET

Untitled

a guest Oct 19th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. struct Reactangle {
  2.     width: i32,
  3.     height: i32
  4. }
  5.  
  6. impl Reactangle {
  7.     // new realmente es un m├ętodo statico que puede llevar
  8.     // cualquier nombre
  9.     fn new(w: i32, h: i32) -> Self {
  10.         Self{
  11.             width: w,
  12.             height: h
  13.         }
  14.     }
  15.     fn get_area(&self) -> i32{
  16.         self.width * self.height
  17.     }
  18. }
  19.  
  20. fn main(){
  21.     // Ahora en lugar de instanciar directament el struct
  22.     // llamamos al metodo new
  23.     let rectangle = Reactangle::new(100,100);
  24.     println!("Rectangle area is {}", rectangle.get_area());
  25.     //OUTPUT Rectangle area is 10000
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top