title: How to get the equivalent of C++ initializer lists
body: <p>I have two simple structures like that:</p>
  
  <pre><code>struct Foo {
  }
  
  struct Bar&lt;'a&gt; {
      foo: &amp;'a mut Foo;
  }
  
  impl Foo {
      pub fn new() -&gt; Foo {
          Foo
      }
  }
  
  impl&lt;'a&gt; Bar&lt;'a&gt; {
      pub fn new(foo: &amp;'a mut Foo) -&gt; Bar&lt;'a&gt; {
          Bar {
              foo: foo,
          }
      }
  }
  </code></pre>
  
  <p>So basically the structure <code>Bar</code> needs a reference to a <code>Foo</code> to work on. This is how I use it:</p>
  
  <pre><code>let mut foo = Foo::new();
  let mut bar = Bar::new(&amp;mut foo);
  </code></pre>
  
  <p>Now I want to create a "glue" structure to handle the creation of all these <code>Foo</code>s and <code>Bar</code>s. So I just want the structure <code>Container</code> to have 1 <code>Foo</code>, 1 <code>Bar</code>.</p>
  
  <p>I tried doing something like:</p>
  
  <pre><code>impl Container {
      pub fn new() -&gt; Container {
          Container {
            foo: Foo::new(),
            bar: Bar::new(&amp;mut foo),
          }
      }
  }
  </code></pre>
  
  <p>But it doesnt work, I cannot reference the field <code>foo</code> from the <code>bar</code> initializer.
  I basically want to mimic the following C++ code:</p>
  
  <pre><code>Container::Container() :
      foo(),
      bar(&amp;foo)
  {}
  </code></pre>
  
  <p>Any ideas on how to solve this? Of course using any kind of dynamic allocation/reference counting would be highly overkill here.</p>
  
  <p><strong>--- EDIT ---</strong></p>
  
  <p>To be clear, I want to create the Rust code equivalent to this C++ snippet:</p>
  
  <pre><code>struct Foo {
  };
  
  struct Bar {
      Bar(Foo&amp; f) : foo(f) {}
      Foo&amp; foo;
  };
  
  Foo f;
  Bar b(f);
  
  struct Container {
      Container() : f(), b(f) {}
      Foo f;
      Bar b;
  };
  </code></pre>
  
  <p><strong>--- EDIT ---</strong></p>
  
  <p>Here is what I ended up doing, using <code>Rc&lt;&gt;</code>.</p>
  
  <pre><code>use std::rc::Rc;
  
  struct Foo {
    pub bar: Rc&lt;Bar&gt;,
  }
  
  impl Foo {
    pub fn new(bar: Rc&lt;Bar&gt;) -&gt; Foo {
      Foo {
        bar: bar,
      }
    }
  }
  
  struct Bar {
    a: u8,
  }
  
  impl Bar {
    pub fn new() -&gt; Bar {
      Bar {
        a: 42,
      }
    }
  }
  
  struct Container {
    pub bar: Rc&lt;Bar&gt;,
    pub foo: Foo,
  }
  
  impl Container {
    pub fn new() -&gt; Container {
      let bar = Rc::new(Bar::new());
      Container {
        bar: bar.clone(),
        foo: Foo::new(bar.clone()),
      }
    }
  }
  
  fn main() {
    // Just checking that we get the same bar for both
    // inside Container and inside Foo
    let c = Container::new();
    println!("{:p}", &amp;*c.bar);
    println!("{:p}", &amp;*c.foo.bar);
  
    // So from what I understand, here bar is on the stack
    // then byte-copied to the heap where that Rc&lt;&gt; will point?
    let bar = Bar::new();
    println!("{:p}", &amp;bar);
    let foo = Foo::new(Rc::new(bar));
    println!("{:p}", &amp;*foo.bar);
  
    // Sad story is that using this Rc&lt;&gt; I now use the "cuteness"
    // and safety that I had with reference borrowing:
    // struct Foo&lt;'a&gt; {
    //   bar: &amp;'a mut Bar,
    // }
    // impl&lt;'a&gt; Foo&lt;'a&gt; {
    //   pub fn new(bar: &amp;'a Bar) -&gt; Foo&lt;'a&gt; {
    //     Foo { bar: bar }
    //   }
    // }
    // let bar = Bar::new();
    // let foo = Foo::new(&amp;bar);
  }
  </code></pre>
  
  <p>But this is not really satisfying, I feel like I used a machine gun to kill a rabbit. Any insight greatly appreciated :(</p>