Build your first Anki deck with Rust
From a short Rust program to a Spanish flashcard you can open in Anki.
When your notes already live in a script, a dataset, or a collection of files, generating an Anki deck can make the authoring process easier to repeat. Your source stays reviewable in Git, and each export becomes a step you can run again.
Start with one word: hola. We want a card that shows the Spanish greeting on the front and its English meaning on the back.
A complete program
This is the repository’s minimal Rust example:
use anki_forge::prelude::*;
fn main() -> anyhow::Result<()> {
let mut deck = Deck::new("Spanish");
deck.basic()
.note("hola", "hello")
.stable_id("es:hola")
.add()?;
deck.write_apkg("spanish.apkg")?.ensure_success()?;
Ok(())
}Deck::new("Spanish") creates the deck. .basic().note("hola", "hello") creates a note using the built-in Basic type. .add() adds it to the deck, and write_apkg(...) writes the package.
The final ensure_success() checks the build report. It lets a script or build job exit with an error when the export reports a problem.
Run it from source
Follow the current quickstart for prerequisites and the source-checkout command. To use the same program in your own project, follow Add to your application.
After exporting, import spanish.apkg into Anki and reveal the answer to your first card.
The Basic example on this site also has a downloadable package if you want to inspect the result first.
Give the note an identity
The string es:hola is an explicit stable ID. It describes which note this is, independently of how you phrase the answer. You could later expand “hello” to “hello; hi” and keep the same ID.
Use a distinct ID for each logical note. If your source already has a durable key, such as a vocabulary entry ID, that is a useful starting point. Avoid deriving identity solely from text that you expect to edit.
Updating a distributed deck also needs release history. Keep the previous package or an identity lockfile and follow the documented update workflow.
Grow from one note
For another greeting, call the Basic builder again with a new front, back, and ID. For a missing word in a sentence, use a Cloze note. For richer cards, define your fields and templates with the project API and package images or audio with the notes.
The Rust authoring guide takes the next step through diagnostics, media, and repeatable builds. The repository examples provide runnable programs to build on.