Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rust Presets

Sketch can be used to define and generate presets for rust crates (with the sketch rust crate command) and for individual Cargo.toml manifests (with the sketch rust manifest command).

The sketch rust crate command mimics the cargo new command in the sense that it detects if these is a workspace manifest in the parent directory of the target output, and if one is detected, the new crate will be added to its members. Also, if certain fields such as lints, keywords, license and so on are set in the workspace manifest but not in the manifest that is generated, they will be added as entries with workspace = true.

Cargo.toml presets are extensible. Dependencies are deeply merged, so for example the features will be joined.

Example

Config:

rust:
  manifest_presets:
    base:
      package:
        version: "0.1.0"
        edition: "2024"

    cli-custom:
      extends_presets:
        - base
        - cli-tools
        - serde-ordered
      package:
        name: cli-custom
      dependencies:
        clap:
          features:
            - derive
        owo-colors:
          features:
            - supports-colors

    cli-tools:
      dependencies:
        ratatui: "0.29"
        clap: "4.5"
        serde: "1"
        owo-colors: "4"

    serde-ordered:
      dependencies:
        serde:
          version: "1"
          features:
            - preserve_order
        indexmap:
          version: "2.11"
          features:
            - serde

Command:

sketch rust manifest cli-custom cargo-example.toml

Output:

[package]
name = "cli-custom"
version = "0.1.0"
edition = "2024"

[dependencies]
clap = { version = "4.5", features = ["derive"] }
indexmap = { version = "2.11", features = ["serde"] }
owo-colors = { version = "4", features = ["supports-colors"] }
ratatui = "0.29"
serde = { version = "1", features = ["preserve_order"] }