Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.84 KB | None | 0 0
  1. /*
  2. I'm learning Rust by making a game.
  3.  
  4. This game would be very simple to make, unsafely, in C/C++,
  5. and I'm hoping that by making it in Rust, I'll learn the core concepts
  6. of ownership and such.
  7.  
  8. But right now, I'm stuck.
  9.  
  10. Here's what I'm *trying* to do, in C++:
  11.  
  12. struct Bar {
  13.     std::string title;
  14.     std::vector<Bar> bars;
  15.     Bar *parent;
  16.  
  17.     Bar *add_bar();
  18. }
  19.  
  20. struct Foo {
  21.     Bar root_bar;
  22.     Bar *current_bar;
  23.  
  24.     void set_current_bar(Bar *bar);
  25.     void add_bar();
  26.     void process();
  27. }
  28.  
  29. And here's what I have so far in Rust:
  30. */
  31.  
  32. // container object for the entire game
  33. struct Foo<'a> {
  34.    root_bar: Bar,
  35.    current_bar: Option<&'a Bar>
  36. }
  37.  
  38. // thing inside game, only exists within the confines of a Foo
  39. struct Bar {
  40.     title: String,
  41.     bars: Vec<Bar>,
  42.     parent: Option<Box<Bar>>
  43.  
  44. }
  45.  
  46. impl<'a> Foo<'a> {
  47.     fn new() -> Self {
  48.         Foo {
  49.             root_bar: Bar {
  50.                 title: "root".to_string(),
  51.                 bars: Vec::new(),
  52.                 parent: None
  53.             },
  54.             current_bar: None,
  55.         }
  56.     }
  57.  
  58.     fn set_current_bar(&self, bar: &Bar) {
  59.         // change which Bar `current_bar` "points" at
  60.         // (it will be used internally in this Foo)
  61.     }
  62.  
  63.     fn add_bar(&self) {
  64.         // add a Bar to `current_bar`'s `bars`,
  65.         // then call set_current_bar() w/ the new Bar
  66.     }
  67.  
  68.     fn process(&self) {
  69.         // "game loop" goes in here
  70.     }
  71. }
  72.  
  73. impl Bar {
  74.     fn add_bar(&self) -> &Bar {
  75.         // create a new Bar
  76.         // set new Bar's `parent` to this,
  77.         // push new Bar to this Bar's `bars`
  78.         // return reference to the new Bar, in `bars`
  79.  
  80.         unimplemented!() // to keep the compiler from complaining
  81.     }
  82. }
  83.  
  84.  
  85. fn main() {
  86.     // create the Foo container and run the game
  87.     Foo::new().process();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement