Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I'm learning Rust by making a game.
- This game would be very simple to make, unsafely, in C/C++,
- and I'm hoping that by making it in Rust, I'll learn the core concepts
- of ownership and such.
- But right now, I'm stuck.
- Here's what I'm *trying* to do, in C++:
- struct Bar {
- std::string title;
- std::vector<Bar> bars;
- Bar *parent;
- Bar *add_bar();
- }
- struct Foo {
- Bar root_bar;
- Bar *current_bar;
- void set_current_bar(Bar *bar);
- void add_bar();
- void process();
- }
- And here's what I have so far in Rust:
- */
- // container object for the entire game
- struct Foo<'a> {
- root_bar: Bar,
- current_bar: Option<&'a Bar>
- }
- // thing inside game, only exists within the confines of a Foo
- struct Bar {
- title: String,
- bars: Vec<Bar>,
- parent: Option<Box<Bar>>
- }
- impl<'a> Foo<'a> {
- fn new() -> Self {
- Foo {
- root_bar: Bar {
- title: "root".to_string(),
- bars: Vec::new(),
- parent: None
- },
- current_bar: None,
- }
- }
- fn set_current_bar(&self, bar: &Bar) {
- // change which Bar `current_bar` "points" at
- // (it will be used internally in this Foo)
- }
- fn add_bar(&self) {
- // add a Bar to `current_bar`'s `bars`,
- // then call set_current_bar() w/ the new Bar
- }
- fn process(&self) {
- // "game loop" goes in here
- }
- }
- impl Bar {
- fn add_bar(&self) -> &Bar {
- // create a new Bar
- // set new Bar's `parent` to this,
- // push new Bar to this Bar's `bars`
- // return reference to the new Bar, in `bars`
- unimplemented!() // to keep the compiler from complaining
- }
- }
- fn main() {
- // create the Foo container and run the game
- Foo::new().process();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement