You use Rust with Godot by leveraging the gdext crate (formerly godot-rust), which generates safe, idiomatic Rust bindings for the Godot engine via the GDExtension API. This allows you to write game logic in Rust, compile it into a native binary, and load it as a plugin within the Godot editor without needing to modify the engine source.
To get started, initialize a new Rust project with the gdext template using cargo-gdext, which handles the complex build configuration and manifest generation automatically. You define your game nodes as Rust structs annotated with #[derive(GodotClass)], then implement the GodotExt trait to handle lifecycle events like _ready and _process.
Here is a minimal example of a Rust script that prints a message every frame:
use godot::prelude::*;
#[derive(GodotClass)]
#[class(init, base=Node2D)]
struct MyNode {
#[base]
base: Base<Node2D>,
}
#[godot_api]
impl INode2D for MyNode {
fn ready(&mut self) {
info!("MyNode is ready!");
}
fn process(&mut self, _delta: f64) {
info!("Processing frame in Rust!");
}
}
After writing your code, you build the extension for your specific target platform using the cargo-gdext command. This compiles the Rust code into a shared library (.so, .dll, or .dylib) and places it in the correct directory for Godot to load.
# Build for the current platform (e.g., Linux x86_64)
cargo gdext build
# Run the Godot editor with the built extension
cargo gdext run
The gdext ecosystem handles the heavy lifting of type conversion between Rust and Godot's internal types, ensuring memory safety and preventing common pitfalls associated with manual FFI. You can access all standard Godot nodes, signals, and properties directly from Rust. For instance, you can connect signals using the connect method on a Gd<T> handle, and the compiler will enforce correct argument types.
Remember that gdext is currently in active development, so you must match your gdext crate version with your Godot engine version (e.g., gdext 0.1.x for Godot 4.2). The workflow is seamless: edit Rust code, run cargo gdext build, and the changes are immediately available in the running Godot editor without restarting the engine, provided you use the hot-reload features available in recent versions. This setup gives you the performance and safety of Rust while retaining the full visual workflow of the Godot editor.