title: How to return a reference to a nested Rust struct in WebAssembly?
body: <p>I have 2 structs where I can access the <code>Parent</code> struct from JavaScript and create it with <code>new</code> method:</p>
  <pre><code>struct Child {
      foo: String,
  }
  
  impl Child {
      pub fn do_something(&amp;self) {
          /* some stuff */
      }
  }
  
  #[wasm_bindgen]
  pub struct Parent {
      child: Child,
  }
  
  #[wasm_bindgen]
  impl Parent {
      pub fn new() -&gt; Self {
          Self {
              child: Child { foo: String::new() },
          }
      }
  
      pub fn child(&amp;self) -&gt; &amp;Child {
          &amp;self.child
      }
  }
  </code></pre>
  <p>I cannot compile the code because of an error at the <code>child()</code> method:</p>
  <pre class="lang-none prettyprint-override"><code>cannot return a borrowed ref with #[wasm_bindgen]
  </code></pre>
  <p>Is there any way to get a pointer to <code>child</code> and access it from JavaScript? Why can Rust not return a borrowed reference to WebAssembly? Is this because JavaScript could modify its value and Rust couldn't guarantee immutability of the data?</p>