Rust Data Modelling Without Classes

preview_player
Показать описание
Today we're talking about how to design your projects in Rust without using inheritance.

Thanks very much to today's sponsor Quadratic.

🖊️ Corrections are in the pinned ERRATA comment.

🙏🏻 CREDITS & PROMO
My name is Tris Oaten and I produce fast, technical videos.

👏🏻 Special thanks to my patreon sponsors:
- JC Andrever-Wright
- Miah Beach
And to all my patrons!
Рекомендации по теме
Комментарии
Автор

ERRATA

- 4:12 Replace -> Replaced

NoBoilerplate
Автор

I’m going through “Writing an Interpreter in Go” (Thorsten, 2018) but instead of Go I’m writing the program in Rust, translating the code as it’s presented. The Rust version using enums is *so much cleaner* then the class based system presented, and I get to skip whole sections when I realize that he’s implementing something that I already have for free. I’d highly recommend the exercise.

hotfishdev
Автор

As a university student looking for an excuse to learn Rust, I want to thank you so much for making concepts in your videos easier to understand!

DavidL
Автор

This is my favourite part of rust! As someone who works within robotics, representing state and transitions is vital, both for safety and for ensuring confidence that the system will be in well defined states for the lifetime of the program.

With so many other languages, a small change in the represented state would correspond to an unknown amount of work to correct the system behaviour elsewhere, as you just don't know that it's wrong until you reach that part of the code.

Gaivs
Автор

Your long search for a good state machine example was certainly worth it! Not sure how you just casually looked at the super mario world manual though haha, but I'm glad you did! Thank you as always for your great content!

JFelipeBa
Автор

When I was first starting to learn Rust coming from Python I was struggling to see the benefit of enums, but then I realized you HAVE to use enums to represent certain data structures. At first that felt restrictive, but then I realized that restriction relieved so much mental burden I'd have otherwise spent in Python thinking about the structure of the program just so that I, the one writing the program, would be able to understand it.

In Rust more than any other language I feel like I can just DO things, rush them even, and the compiler is like R2-D2 was to Luke Skywalker, guiding me and filling the gaps I'm unable to fully grasp in my limited human working memory. When my code does start to get unwieldy, refactoring isn't some mess where everything becomes more confusing, often as a program grows it seems little slots arise where you know a certain type (like an enum) clicks in like a puzzle piece.

My two cents of advice to people who want to learn Rust coming from more OO languages:

1. ```struct != class```: Be careful with Impl block methods and the "self" keyword. Structs are purely data and methods are syntactic sugar for functions, as they always are, but this relationship is much closer to the surface in Rust than it is in other OO languages and you'll hit some frustrating roadblocks if you accept "struct == class" at face value.

2. Avoid Traits except where the compiler tells you you need them or an API requires them. (i.e. #[derive(Clone, Copy, Debug)] ). As you learn you will find places where they fill a niche, but shoehorning them in as a replacement for c++ templates/js interfaces/python protocols is a recipe for unreadable spaghetti code.

3. USE ENUMS. ALWAYS, ALWAYS, ALWAYS. They are fucking awesome, as this video handily outlines.

patrick
Автор

Having learned Haskell just before Rust, enums felt just right. And since now having spent a lot more time in Rust, I always feel good when I use a nice match expression! Thanks Tris!

Killzre
Автор

I love enums, they're a really nice way to represent "choices" in the type system, rather than writing ugly if statements that try to verify everything with a bunch of bools

pixelstriko
Автор

That Mario example is what I've needed for months! That's so good. And tying it to the actual code, chefs kiss. I finally get state machines, and I have a tangible idea of how it can be made. Thank you!

onerimeuse
Автор

One of my favorite YouTube channels even though I don’t really watch the videos. It’s just something about the overall style and personality that bleeds through them. There’s no filler and there’s no commercialism. It just feels like a sincerely enthusiastic person trying to tell you something clearly that actually is of interest. It definitely hugely has led me to be sure to check out more Rust whenever the opportunity is there. I still hear his swinging British voice in my head when I try it, like, “In Rust, all you have to do is read the compiler message carefully.”

PromptStreamer
Автор

One of my favourite applications for state machines is fighting game input parsing. In a game like Street Fighter, a particular special move might require a sequence such as down, down-right, right, punch. But the games build in a bit of leniency, so your timing doesn't have to be exact. A great way to read the player's inputs then is to create a state machine for the special move, feeding the last several frames' worth of inputs through it. It makes it easy to define leniency as well as failure states in precise terms.

Автор

I pretty consistently worry that I am watching your videos too soon, having barely started with Rust, and the elegance of your explanations prevents brain fog.

Robstafarian
Автор

I've instinctively tried to use this in other languages, only to find despair. Rust is just so fantastic.

AceofSpades
Автор

Interesting to see a nice algebraic type system and exhaustive pattern matching outside of a functional language. This was always my favorite feature of F#.

ycombine
Автор

Your videos spiked so much interest for Rust in me that I ditched the plan for learning Golang. Reading the Chapter 10 in TheBook now.

SushantShekhar-myeu
Автор

I always wonder why a lot of popular languages don't have sum types natively built in. It's like.. one of the most fundamental way to model data

detaaditya
Автор

I don't know Rust but it's really fun pausing your videos on code snippets and compiler errors, trying to figure out what it means and being successful almost every time!

geeshta
Автор

As someone who is currently learning rust, i just finished the chapter on enums from “the book”, and i was able to understand and comprehend whats going on, hats off to you for explaining a what seems like complicated topic in such an elegant way !! This was both a useful rust video and an explanation of how machine state logic works! Amazing

jayedr
Автор

I love algebraic type systems so much!! Haskell made me fall in love with them. I love this!!

kellybmackenzie
Автор

This is very informative. I've always struggled with data modelling, in rust, in particular. Will be saving this for many rewatchs. As always, thanks for the quality content.

ahmed.systems