title: Returning a Rust Vec<Vec<i32>> to JavaScript via WebAssembly
body: <p>I am working on a project involving Rust and WebAssembly using the "wasm32-unknown-unknown" target. It is no problem to return a <code>Vec&lt;i32&gt;</code> from my Rust code using this function:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[no_mangle]
  pub extern "C" fn calc_vector() -&gt; usize {
      unsafe {
          vec_len = 0;
      }
  
      let mut data: Vec&lt;i32&gt; = Vec::new();
  
      for i in 0..1000 {
          data.push(i);
      }
  
      unsafe {
          vec_len = data.len();
      }
  
      data.as_mut_ptr() as usize
  }
  </code></pre>
  
  <p>This returns an offset and I call another function from JS to get the length of my <code>Vec</code>. I then build the Vector in JavaScript again (knowing that <code>i32</code> -> 4x <code>uint8</code>):</p>
  
  <pre><code>let vec_addr = exports.calc_vector();
  let vec_len = exports.get_vec_len();
  
  while(arr.length &lt; vec_len) {
      let numberUint8 = new DataView(view.buffer, vec_addr, 4);
      let number = numberUint8.getInt32(0, true);
      arr.push(number)
  
      // move to next value in vector
      vec_addr += 4;
  }
  </code></pre>
  
  <p>Using this, I want to create a Rust function that returns a <code>Vec&lt;Vec&lt;i32&gt;&gt;</code>, however, it just won't work:</p>
  
  <pre class="lang-rust prettyprint-override"><code>#[no_mangle]
  pub extern "C" fn calc_vector_in_vector() -&gt; usize {
      unsafe {
          vec_len = 0;
          elements_in_vect = 0;
      }
  
      let mut outer_vec: Vec&lt;*mut i32&gt; = Vec::new();
      let mut inner_vec: Vec&lt;i32&gt; = Vec::new();
  
      for i in 0..100 {
          inner_vec.push(i);
          unsafe {
              elements_in_vect += 1;
          }
      }
  
      outer_vec.push(inner_vec.as_mut_ptr());
  
      unsafe {
          vec_len = outer_vec.len();
      }
  
      outer_vec.as_mut_ptr() as usize
  }
  </code></pre>
  
  <p>I thought that I could use the same logic as with a single <code>Vec</code>: at the address returned from <code>calc_vector_in_vector()</code> is the first entry of the outer vector, containing the address as <code>i32</code> of the first element of the inner vector (an actual number). However, on this address there seems to be nonsense. What I am doing wrong here? </p>