Skip to content

RashingPro/rust-workspace-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Workspace Template

This project is a template containing Cargo workspace. Use it if you want to split your big project into several crates, but share same settings and build it all together.

Adding new crate

  1. Create folder containing Cargo.toml and either src/main.rs or src/lib.rs
  2. Cargo.toml content:
    [package]
    name = "example"
    
    version.workspace = true
    description.workspace = true
    documentation.workspace = true
    homepage.workspace = true
    repository.workspace = true
    authors.workspace = true
    keywords.workspace = true
    categories.workspace = true
    
    edition.workspace = true
    rust-version.workspace = true
    
    [lints]
    workspace = true
    
    [dependencies]
  3. Add new crate name to workspace.members in root Cargo.toml
  4. If this crate is needed to be dependency of other crates
    • Add following line to workspace.dependencies in root Cargo.toml
      [workspace.dependencies]
      example = { path = "example" }
    • In dependent crate, add this to dependencies
      [dependencies]
      example.workspace = true

Adding external dependency

You should not add external dependency directly in crate's Cargo.toml. Instead, add it in root Cargo.toml:

[workspace.dependencies]
abc = "1.2.3"
# Or
abc = { version = "1.2.3" }

Then, add following line to crate's Cargo.toml

[dependencies]
abc.workspace = true
# Or
abc = { workspace = true }

Linting and formatting

Via command

To lint and format your whole project, use:

cargo clippy
cargo fmt

Q: Why we use nightly toolchain?
A: Because some features, like disabling trailing_comma only available with nightly toolchain.
Note: it might require you to install nightly toolchain. In most cases, use rustup toolchain install nightly

JetBrains RustRover

Settings > Rust > External Linters

  • Enable Run external linter on the fly
  • Select Clippy as linter
  • Select [default] under Channel selector

Settings > Rust > Rustfmt

  • Select [default] under Channel selector
  • Enable Use Rustfmt instead of the built-in formatter
  • Click Configure actions on save and enable following:
    • Reformat code
    • Rearrange code
    • Run code cleanup

About

Cargo workspace template

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages