• 0 Posts
  • 7 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.






  • Regex is actually just a way to write (Epsilon) non determistic state automata(ε-NDA) using text! ε-NDA comes from automata theory and they are just a somewhat powerful way to describe state machines! They can kind of be seen as a stepping stone to things like Context-Free Grammars which is what language parsers use to define their language/parsers, and Turing machines! Regex is a fundamental part of computer science, and they are of course incredibly useful in string validation due to their expressive power! If you study at uni and get the chance to take a course in automata theory I recommend it! Personal favorite subject :)