Hello,
Welcome to the First Edition of Our Embedded Rust Newsletter!
In this edition, we'll dive into setting up a Cargo workspace specifically tailored for embedded Rust development. Whether you're building firmware for microcontrollers or developing embedded systems, a well-organized workspace can make your development process smoother and more efficient. Let's get started!
1. Introduction to Cargo Workspaces
Cargo workspaces are a powerful feature of Cargo, Rust’s package manager and build system. They allow you to manage multiple packages (crates) in a single workspace, making it easier to handle complex projects with multiple components.
2. Prerequisites
Before setting up your workspace, ensure you have the following:
Rust Toolchain: Install Rust and Cargo via rustup.
Embedded Toolchains: Install relevant toolchains and target support for your embedded hardware.
3. Setting Up Your Cargo Workspace
Create the Root Directory
Start by creating a root directory for your workspace. This directory will contain the
Cargo.tomlfile for the workspace and subdirectories for individual crates.
mkdir bootstrap-ws
cd bootstrap-ws/Set the Workspace
Create a Cargo.toml file in the root directory with the list of packages as below:
bootstrap-ws $cat Cargo.toml
[workspace]
members = [
"execs",
"libs",
]bootstrap-ws $
This specifies execs and libs packages are part of this workspace. The idea here is to keep the executables in the execs package (crate) and the libs package the libraries. The crates here are also called sub projects. As you see here, the benefit of using workspace is it can include both binary and library crate in it.
Create execs and libs crate
For newbies, in Rust, a library crate is a type of crate that provides reusable code in the form of libraries, which can be shared and used by other Rust projects and a binary crate produces the executables . Thus by combining both in workspace, its easy to organize and it caters all needs for any embedded project.
cargo new execs
cargo new libs --libEach of above crates have its own Cargo.toml and source files. The directory & file structure looks as below:
boostrap-ws $tree
.
├── Cargo.toml
├── execs
│ ├── Cargo.toml
│ └── src
│ └── main.rs
└── libs
├── Cargo.toml
└── src
└── lib.rs
4 directories, 5 filesThe workspace (entire project) can be built with cargo build command.
boostrap-ws $cargo buildIn next issue, we’ll explore how to configure custom linker scripts and targets for embedded Rust development.