You've got a C codebase you'd like to move to Rust, but you're not sure where to
start?
This course is for you.
You'll learn techniques for migrating C codebases to Rust by solving exercises.
You'll learn how to maintain a safe mixed C-Rust codebase, incrementally migrate
modules, translate common C idioms to Rust, and use the debugging tools you'll
need when things inevitably break — one exercise at a time.
Note
This course has been written by
Mainmatter.
It's one of the workshops in
our portfolio of Rust workshops.
Check out our landing page if
you're looking for Rust consulting or training.
Clone this repository to your local machine:
git clone https://github.com/mainmatter/migrating-c-to-rustWe recommend you work on a branch, so you can easily track your progress and pull in updates from the main repository, if needed:
cd migrating-c-to-rust
git checkout -b my-solutionsAll exercises are located in the exercises folder. Each exercise is structured
as a Rust package. The package contains the exercise itself, instructions on
what to do (in src/lib.rs), and a test suite to automatically verify your
solution.
To verify your solutions, we've provided a tool to guide you through the course:
the wr CLI, short for "workshop runner". Install wr by following the
instructions on
its website.
Once you have wr installed, open a new terminal, navigate to the top-level
folder of the repository, and run:
wrwr will verify the solution to the current exercise. Don't move on to the next
section until you've solved the exercise for the current one.
-
Rust, installed through
rustup. The repository pins a single nightly inrust-toolchain.toml: Miri needs nightly, and the date matches the onecheadergenrequires. Runrustup toolchain installfrom the repository root to install it. -
A C compiler. The one provided by your operating system will be good enough.
-
libclang, which
bindgenuses to parse C headers. Many systems already have it; if yours doesn't, followbindgen's requirements page. -
cheadergen, which generates C headers from Rust code. Install the CLI crate,cheadergen_cli(cargo install cheadergenfails: that crate is the library):cargo install --locked cheadergen_cli --version 0.3.3
-
Two extra compilation targets. One exercise type-checks your bindings against platforms that disagree with yours, which needs their standard libraries. Run this from the repository root, so the targets are added to the pinned toolchain:
rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu
Nothing is compiled or linked for those targets, so you don't need a cross compiler.
-
(Optional but recommended) An IDE with Rust autocompletion support. We recommend one of the following:
- RustRover;
- Visual Studio Code with the
rust-analyzerextension.
Copyright © 2024- Mainmatter GmbH (https://mainmatter.com), released under the PolyForm-Noncommercial-1.0.0.
If you're planning to use this course internally at your company, please reach out for a license. We also offer this course as an instructor-led workshop.