• 0 Posts
  • 2 Comments
Joined 1 year ago
cake
Cake day: August 9th, 2023

help-circle

  • Late response and you might have already gotten an answer, but what you wrote is exactly the same as:

    // Define our player struct 
    struct Player {
         x: f32,
         y: f32,
         rotation: f32
    }
    // Define the methods available to the player struct 
    impl Player {
         pub fn move(&mut self, x: f32, y: f32) {
              self.x += x;
              self.y += y;
         }
    
         pub fn rotate(&mut self, by: f32) {
               self.rotation += by;
         }
    }
    
    fn main() {
        let mut player = Player { x: 0.0, y: 0.0, rotation: 0.0 };
        player.move(10.0, 10.0);
        player.rotation(180.0);
    }
    

    The code example you wrote does not use anything that is exclusive to OOP languages as you are simply encapsulating values in a class (struct in the Rust case).

    Unlike C++, the biggest difference you will find is that Rust does not have the same kind of inheritance. In Rust you can only inherit from traits (think interfaces in Java/C# or type classes if you have ever used Haskell), whereas in C++ and other OOP languages you can also inherit from other classes. In a lot of cases just using traits will suffice when you need inheritance. :)

    So in conclusion, no global functions! You still have the same name spacing and scoping as you would in C++ etc!

    Ps. I use VScode because it rocks with Rust, and while Rust is heavily inspired by functional programming languages, it is not a pure functional programming language (nor is C) but that is another can of worms.