← 返回命令列表

Linux command

cargo-new 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Create new binary project

cargo new [project_name]

Create new library

cargo new [project_name] --lib

Create without git

cargo new [project_name] --vcs none

Create with specific edition

cargo new [project_name] --edition [2021]

Create with custom name

cargo new [path] --name [custom_name]

说明

cargo new creates a new Cargo package in a new directory. It generates a Cargo.toml manifest file, a src/ directory with either a "Hello, World!" binary (`main.rs`) or a library stub (`lib.rs`), and initializes a git repository by default. The generated Cargo.toml includes the package name, version, and Rust edition. The command respects Cargo configuration for default settings such as author name and email, which are read from git config or environment variables. Custom templates are not supported; use `cargo-generate` for template-based project creation.

参数

--bin
Create binary with src/main.rs (default)
--lib
Create library with src/lib.rs
--name _name_
Package name (defaults to directory name)
--edition _year_
Rust edition (2015, 2018, 2021, 2024). Defaults to latest stable edition.
--vcs _type_
Version control system (git, hg, pijul, fossil, none)
--registry _name_
Registry for publishing
-v, --verbose
Verbose output
-q, --quiet
Suppress output
--color _when_
Coloring: auto, always, never.
--offline
Run without accessing the network.

FAQ

What is the cargo-new command used for?

cargo new creates a new Cargo package in a new directory. It generates a Cargo.toml manifest file, a src/ directory with either a "Hello, World!" binary (`main.rs`) or a library stub (`lib.rs`), and initializes a git repository by default. The generated Cargo.toml includes the package name, version, and Rust edition. The command respects Cargo configuration for default settings such as author name and email, which are read from git config or environment variables. Custom templates are not supported; use `cargo-generate` for template-based project creation.

How do I run a basic cargo-new example?

Run `cargo new [project_name]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --bin do in cargo-new?

Create binary with src/main.rs (default)