Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement