logoalt Hacker News

scuff3dtoday at 5:46 PM1 replyview on HN

You can store state in the struct and then define a method in the trait to return the struct. Then all your default methods can use the "getter" to access the struct and it's state. The only thing you have to do is embed the struct and implement that one "getter" method to return it. I don't think it's much more boilerplate then utilizing inheritance.

Fyrox, a game engine written in Rust, uses an ECS and several object oriented patterns in their design. Might be a good reference if your interested. The rust book also has a section on OOP patterns in Rust.

I think it's Fyrox anyway. I remember the creator of a Rust game engine talking about it in an interview on Developer Voices. It could have been Bevy I guess, but I don't think so.


Replies

reactordevtoday at 6:11 PM

Yeah that’s what I did and it’s ugly. It works, and allows me to attach multiple behaviors but I would have to initialize them and write that boilerplate code to return them.

I think I might be able to do it with a macro but I’m not a rust guy so I’m limited by my knowledge.