← 返回命令列表

Linux command

cargo-init 命令

文本

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

常用示例

Initialize in current directory

cargo init

Initialize binary project

cargo init --bin

Initialize library

cargo init --lib

Initialize without git

cargo init --vcs none

Initialize with specific name

cargo init --name [myproject]

Initialize with specific edition

cargo init --edition [2021]

Initialize in a specific directory

cargo init [path/to/dir]

说明

cargo init creates a new Cargo package in an existing directory. Generates Cargo.toml and src/ with either main.rs (binary) or lib.rs (library). Unlike cargo new, does not create a new directory.

参数

--bin
Create binary target with src/main.rs (default).
--lib
Create library target with src/lib.rs.
--name _name_
Set package name (defaults to directory name).
--edition _year_
Rust edition (2015, 2018, 2021, 2024).
--vcs _type_
Initialize version control (git, hg, pijul, fossil, none).
--registry _name_
Registry for publishing.
--force
Force overwrite of existing Cargo.toml or src directory.
-v, --verbose
Verbose output.
-q, --quiet
Suppress output.

FAQ

What is the cargo-init command used for?

cargo init creates a new Cargo package in an existing directory. Generates Cargo.toml and src/ with either main.rs (binary) or lib.rs (library). Unlike cargo new, does not create a new directory.

How do I run a basic cargo-init example?

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

What does --bin do in cargo-init?

Create binary target with src/main.rs (default).