Advertisement
AntonioVillanueva

Un ejemplo de "Clase" en rust

Jan 15th, 2025
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.52 KB | None | 0 0
  1. /*Algo parecido a una clase en Rust
  2.  * Calculo del area de un rectangulo
  3.  */
  4. #[warn(dead_code)]
  5. //#[derive(Debug)]
  6.  
  7. struct Rectangle {
  8.     x:u32,
  9.     y:u32,
  10. }
  11.  
  12. impl Rectangle{
  13.     fn new (x:u32,y:u32)->Self {
  14.         Self{x,y}
  15.     }
  16.    
  17.     fn area(&self)->u32{
  18.         self.x*self.y
  19.     }
  20.    
  21.     fn get_x(&self)->u32{
  22.         self.x
  23.     }
  24.    
  25.     fn get_y(&self)->u32 {
  26.         self.y
  27.     }
  28. }
  29.  
  30. fn main (){
  31.     let rectangle = Rectangle::new(2,5);
  32.     println! ("x :{}",rectangle.get_x());
  33.     println! ("y :{}",rectangle.get_y());  
  34.     println! ("{}",rectangle.area());
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement