R
RUSTY YELLOW PAGES

cli_lib

A workspace with a console crate and a library crate, the path dependency and the call between them already written.


What it generates

  • <name>_cli — a binary crate that depends on the library by path and calls its example method
  • <name>_lib — a library crate holding one example method and the unit test for it

Source: cli_lib in the templates repository.

cargo new cannot produce this layout: --bin and --lib are mutually exclusive. The layout matters because a binary target cannot be integration-tested — tests/ links against a library target, so code in main.rs is reachable only from unit tests in that same file. Putting the logic in a library crate and leaving the binary as a caller is the usual way around it.

The crates sit in a workspace under crates/. The binary declares the library as a path dependency and calls it from main.

Three settings it gets right, each easy to miss when writing a workspace by hand:

resolver = "3" — a virtual manifest without it falls back to resolver 1, and cargo emits a warning on every build.

[workspace.package] — version and edition are declared once and taken by each crate with version.workspace = true, rather than copied.

Underscores in crate names — hyphens are valid in a package name but not in a Rust identifier, so a hyphenated package is spelled one way in Cargo.toml and another in use.


One-time setup

Two commands, once per machine. cargo-generate is a subcommand of its own rather than part of cargo. --locked builds it against the dependency versions its author released it with; without it, cargo resolves to the newest and the build can fail on a compiler older than whatever those now require.

cargo install cargo-generate --locked

Then file this repository under a name, so no command after it has to carry the URL. It writes $CARGO_HOME/cargo-generate.toml, creating the file if it is not there. The name is yours to pick; the commands here use ryp.

macOS, Linux, Git Bash

printf '[favorites.ryp]\ngit = "https://github.com/NGDeveloper125/rusty-yellow-pages-templates"\n' >> ~/.cargo/cargo-generate.toml

Windows PowerShell

Add-Content "$HOME\.cargo\cargo-generate.toml" @('[favorites.ryp]', 'git = "https://github.com/NGDeveloper125/rusty-yellow-pages-templates"')

Generating it

Crate names default to the project name

cargo generate ryp cli_lib --name inventory_tool

Or name either crate yourself

cargo generate ryp cli_lib --name shop -d cli_name=console -d lib_name=engine

Without that favourite, the repository goes in the command instead.

cargo generate --git https://github.com/NGDeveloper125/rusty-yellow-pages-templates cli_lib --name inventory_tool