Naming conventions
snake_case to camelCase
The code styles are very different between Rust and JavaScript. The Rust community prefers the snake_case style while the JavaScript community prefers the camelCase style. NAPI-RS will change the case of the Rust code to the camelCase style automatically.
#[napi]
pub fn a_function(a_arg: u32) -> u32 {
a_arg + 1
}⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️
export function aFunction(aArg: number): numberjs_name
You can use the js_name attribute in #[napi] to rename the JavaScript function.
#[napi(js_name = "coolFunction")]
pub fn a_function(a_arg: u32) -> u32 {
a_arg + 1
}⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️ ⬇️
export function coolFunction(aArg: number): numberThe JavaScript function name will be coolFunction, both in the generated TypeScript definition and in the JavaScript runtime:
import { coolFunction } from './index.js'
console.log(coolFunction(1)) // 2