Hello, Emacs!

Create a new project:

cargo new greeting
cd greeting

Modify Cargo.toml:

edition = "2018"

crate-type = ["cdylib"]

emacs = "0.11.0"

Write code in src/lib.rs:

# #![allow(unused_variables)]
#fn main() {
use emacs::{defun, Env, Result, Value};

// Emacs won't load the module without this.

// Register the initialization hook that Emacs will call when it loads the module.
fn init(env: &Env) -> Result<Value<'_>> {
    env.message("Done loading!")

// Define a function callable by Lisp code.
fn say_hello(env: &Env, name: String) -> Result<Value<'_>> {
    env.message(&format!("Hello, {}!", name))

Build the module and create a symlink with .so extension so that Emacs can recognize it:

cargo build
cd target/debug

# If you are on Linux
ln -s libgreeting.so greeting.so

# If you are on macOS
ln -s libgreeting.dylib greeting.so

Add target/debug to your Emacs's load-path, then load the module:

(add-to-list 'load-path "/path/to/target/debug")
(require 'greeting)
(greeting-say-hello "Emacs")

The minibuffer should display the message Hello, Emacs!.