为什么 `cargo new` 创建二进制文件而不是库?
Why does `cargo new` create a binary instead of a library?
我正在用 Rust 创建一个库。在创建库时,我输入
cargo new name
根据文档,这应该创建一个库,因为省略了 --bin
。
但是,该文件会自动设置为二进制文件。
是否有我必须调整的设置以禁用自动将所有项目设置为二进制文件?
Cargo features
Cargo’s CLI has one really important change this release: cargo new
will now default to generating a binary, rather than a library. We try to keep Cargo’s CLI quite stable, but this change is important, and is unlikely to cause breakage.
For some background, cargo new
accepts two flags: --lib
, for creating libraries, and --bin
, for creating binaries, or executables. If you don’t pass one of these flags, in previous versions of Cargo, it would default to --lib
. We made this decision because each binary (often) depends on many libraries, and so the library case is more common. However, this is incorrect; each library is depended upon by many binaries. Furthermore, when getting started, what you often want is a program you can run and play around with. It’s not just new Rustaceans though; even very long-time community members have said that they find this default surprising. As such, we’re changing it.
自货运 1.25 cargo new
defaults to creating a binary crate, instead of a library crate。
cargo new accepts two flags: --lib, for creating libraries, and --bin, for creating binaries, or executables.
我正在用 Rust 创建一个库。在创建库时,我输入
cargo new name
根据文档,这应该创建一个库,因为省略了 --bin
。
但是,该文件会自动设置为二进制文件。
是否有我必须调整的设置以禁用自动将所有项目设置为二进制文件?
Cargo features
Cargo’s CLI has one really important change this release:
cargo new
will now default to generating a binary, rather than a library. We try to keep Cargo’s CLI quite stable, but this change is important, and is unlikely to cause breakage.For some background,
cargo new
accepts two flags:--lib
, for creating libraries, and--bin
, for creating binaries, or executables. If you don’t pass one of these flags, in previous versions of Cargo, it would default to--lib
. We made this decision because each binary (often) depends on many libraries, and so the library case is more common. However, this is incorrect; each library is depended upon by many binaries. Furthermore, when getting started, what you often want is a program you can run and play around with. It’s not just new Rustaceans though; even very long-time community members have said that they find this default surprising. As such, we’re changing it.
自货运 1.25 cargo new
defaults to creating a binary crate, instead of a library crate。
cargo new accepts two flags: --lib, for creating libraries, and --bin, for creating binaries, or executables.