Skip to content

Instantly share code, notes, and snippets.

@rjoydip-zz
Created July 16, 2020 17:23
Show Gist options
  • Save rjoydip-zz/43576dafb4ca482e4065f58c6db19cc5 to your computer and use it in GitHub Desktop.
Save rjoydip-zz/43576dafb4ca482e4065f58c6db19cc5 to your computer and use it in GitHub Desktop.
echo a string in neon (rust & node)

echo a string in neon (rust & node)

The new method on JsString class takes a &str so we add a & before the name of the variable:

use neon::prelude::*;
use neon::register_module;

fn echo(mut cx: FunctionContext) -> JsResult<JsString> {
 let my_str = cx.argument::<JsString>(0)?.value() as String;
 Ok(JsString::new(&mut cx, my_str))
}

register_module!(mut m, {
 m.export_function("echo", echo)?;
 Ok(())
});

In javascript

const addon = require('../native')
console.log(addon.echo('abc')) // 'abc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment