Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Reactangle {
- width: i32,
- height: i32
- }
- impl Reactangle {
- // new realmente es un método statico que puede llevar
- // cualquier nombre
- fn new(w: i32, h: i32) -> Self {
- Self{
- width: w,
- height: h
- }
- }
- fn get_area(&self) -> i32{
- self.width * self.height
- }
- }
- fn main(){
- // Ahora en lugar de instanciar directament el struct
- // llamamos al metodo new
- let rectangle = Reactangle::new(100,100);
- println!("Rectangle area is {}", rectangle.get_area());
- //OUTPUT Rectangle area is 10000
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement