LogoLogo
HomeBlogGitHubGet Started FreeLogin
Earthly 0.7
Earthly 0.7
  • 👋Introduction
  • 💻Install Earthly
  • 🎓Learn the basics
    • Part 1: A simple Earthfile
    • Part 2: Outputs
    • Part 3: Adding dependencies With Caching
    • Part 4: Args
    • Part 5: Importing
    • Part 6: Using Docker In Earthly
    • Part 7: Using remote runners
    • Part 8a: Using Earthly in your current CI
    • Final words
  • ⭐Featured guides
    • Rust
  • 📖Docs
    • Guides
      • Importing
      • Build arguments and secrets
      • Functions
      • Using Docker in Earthly
      • Multi-platform builds
      • Authenticating Git and image registries
      • Integration Testing
      • Debugging techniques
      • Podman
      • Configuring registries
        • AWS ECR
        • GCP Artifact Registry
        • Azure ACR
        • Self-signed certificates
      • Using the Earthly Docker Images
        • earthly/earthly
        • earthly/buildkitd
      • ✅Best practices
    • Caching
      • Caching in Earthfiles
      • Managing cache
      • Caching via remote runners
      • Caching via a registry (advanced)
    • Remote runners
    • Earthfile reference
      • Builtin args
      • Excluding patterns
      • Version-specific features
    • The earthly command
    • Earthly lib
    • Configuration reference
    • Examples
    • Misc
      • Alternative installation
      • Data collection
      • Definitions
      • Public key authentication
  • 🔧CI Integration
    • Overview
    • Use the Earthly CI Image
    • Build your own Earthly CI Image
    • Pull-Through Cache
    • Remote BuildKit
    • Vendor-Specific Guides
      • Jenkins
      • Circle CI
      • GitHub Actions
      • AWS CodeBuild
      • Kubernetes
      • Google Cloud Build
      • GitLab CI/CD
      • Woodpecker CI
      • Bitbucket Pipelines
  • ☁️ Earthly Cloud
    • Overview
    • Managing permissions
    • Cloud secrets
    • Earthly Satellites
      • Managing Satellites
      • Using Satellites
Powered by GitBook
On this page
  • Step 1: Import the Rust library
  • Step 2: Initialize the Rust toolchain
  • Step 3: Build your Rust project
  • Finally

Was this helpful?

Edit on GitHub
  1. Featured guides

Rust

This page will help you use Earthly if you are using Rust.

Step 1: Import the Rust library

To get started, import the rust library as shown below. Note that the --global-cache flag is currently required to allow for adequate caching of the Rust toolchain. This flag will be implied in a future release.

VERSION --global-cache 0.7

IMPORT github.com/earthly/lib/rust:2.2.11 AS rust

Step 2: Initialize the Rust toolchain

Next, initialize the Rust toolchain via rust+INIT. This will install any necessary dependencies that rust+CARGO needs underneath.

install:
  FROM rust:1.73.0-bookworm
  RUN apt-get update -qq
  RUN apt-get install --no-install-recommends -qq autoconf autotools-dev libtool-bin clang cmake bsdmainutils
  RUN rustup component add clippy
  RUN rustup component add rustfmt
  # Call +INIT before copying the source file to avoid installing depencies every time source code changes. 
  # This parametrization will be used in future calls to functions of the library
  DO rust+INIT --keep_fingerprints=true

Step 3: Build your Rust project

Now you can build your Rust project. Collect the necessary sources and call rust+CARGO to build your project.

source:
  FROM +install
  COPY --keep-ts Cargo.toml Cargo.lock ./
  COPY --keep-ts --dir package1 package2  ./

build:
  FROM +source
  DO rust+CARGO --args="build --release" --output="release/[^/\.]+"
  SAVE ARTIFACT ./target/release/*

Notice the need for the --keep-ts flag when copying the source files. This is necessary to ensure that the timestamps of the source files are preserved such that Rust's incremental compilation works correctly.

Additionally, because cargo does not make a good distinction between intermediate and final artifacts, we use the --output flag to specify which files should be extracted from the cache at the end of the operation.

Finally

PreviousFeatured guidesNextGuides

Last updated 1 year ago

Was this helpful?

For a complete Earthfile example on how to use Rust in Earthly, visit the .

See also the reference documentation for , to understand the different parameters used with rust+INIT and rust+CARGO.

⭐
rust example directory on GitHub
lib/rust