title: How to return a string (or similar) from Rust in WebAssembly?
body: <p>I created a small Wasm file from this Rust code:</p>
  
  <pre><code>#[no_mangle]
  pub fn hello() -&gt; &amp;'static str {
      "hello from rust"
  }
  </code></pre>
  
  <p>It builds and the <code>hello</code> function can be called from JS:</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
    &lt;script&gt;
      fetch('main.wasm')
      .then(response =&gt; response.arrayBuffer())
      .then(bytes =&gt; WebAssembly.instantiate(bytes, {}))
      .then(results =&gt; {
        alert(results.instance.exports.hello());
      });
    &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My problem is that the <code>alert</code> displays "undefined". If I return a <code>i32</code>, it works and displays the <code>i32</code>. I also tried to return a <code>String</code> but it does not work (it still displays "undefined").</p>
  
  <p>Is there a way to return a string from Rust in WebAssembly? What type should I use?</p>