Skip to main content

Rust

Rust is a systems programming language focused on safety and performance; feel free to browse the Foundation hosted projects written in Rust.

Build and test​

GitHub-hosted runners include a Rust toolchain managed by rustup. To pin a specific version, add a rust-toolchain.toml file to the repository; rustup picks it up automatically.

The following workflow builds and tests on Linux, macOS and Windows, and caches downloaded crates and build output between runs:

name: CI

on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- run: cargo build --locked
- run: cargo test --locked

Publishing on crates.io​

Crates are published to crates.io with cargo publish.

crates.io supports trusted publishing from GitHub Actions: the workflow exchanges a short-lived OpenID Connect (OIDC) token for a temporary crates.io token, so no long-lived CARGO_REGISTRY_TOKEN needs to be stored as a secret. The rust-lang/crates-io-auth-action gets the temporary token and revokes it when the job finishes.

FINOS owns the crates.io account for FINOS crates, so help@finos.org sets up trusted publishing. It can only be configured for a crate that already exists on crates.io, so the first release of a new crate still needs an API token; contact help@finos.org about that first release too. After that:

  1. Email help@finos.org to add the trusted publisher. Include the crate name, the GitHub repository (for example finos/<repository>), the workflow file name and, if you use one, the GitHub environment name.
  2. Grant id-token: write to the job that publishes.
  3. Pass the token from crates-io-auth-action to cargo publish:
  publish:
runs-on: ubuntu-latest
environment: crates.io # must match the environment in the trusted publisher, if you set one
permissions:
contents: read
id-token: write # crates.io trusted publishing (OIDC)
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Authenticate with crates.io
id: auth
uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9 # v1.0.6
- name: Publish to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
run: cargo publish --locked

For example, morphir-rust publishes each crate from a crates/<name>/v<version> tag, running cargo publish --dry-run before the real publish. Its workflow currently signs in with a CARGO_REGISTRY_TOKEN secret, passed only to the publish step, because crates.io needs a token for the first publish of a crate; the workflow's comments describe moving each crate to trusted publishing in a crates.io environment once it exists on crates.io.

A badge can be added at the top of the project's root-level README.md file, using the following Markdown syntax:

[![Crates.io](https://img.shields.io/crates/v/<crate name>.svg)](https://crates.io/crates/<crate name>)