How can I set default authors for new Cargo projects?
Asked Answered
M

1

14

When creating a new project with cargo new, I would like to have the Cargo.toml file automatically include a predefined authors field.

The Rust book said:

The next four lines set the configuration information Cargo needs to compile your program: the name, the version, who wrote it, and the edition of Rust to use. Cargo gets your name and email information from your environment, so if that information is not correct, fix the information now and then save the file.

It was pretty vague, so I searched about it. First I tried adding CARGO_NAME and CARGO_EMAIL to my environment variables. Didn't work.

Then I tried adding the variables name and email in the field [cargo-new], on the .cargo/config.toml config file and learned it is deprecated.

Are there any other ways to do this? Did I do something wrong?

[package]
name = "hello_world"
version = "0.1.0"
edition = "2018"

authors = ["foo <[email protected]>"] # Add this line automatically

[dependencies]

I'm using rustup with the nightly toolchain on Arch Linux.

Mcardle answered 8/6, 2021 at 17:23 Comment(0)
U
20

The behaviour was changed in RFC 3052, implemented in Cargo 1.53. From the RFC:

cargo init will stop pre-populating the field when running the command, and it will not include the field at all in the default Cargo.toml. Crate authors will still be able to manually include the field before publishing if they so choose.

It turned out that the authors list in a crate's manifest created more problems than it solved, because the manifest is immutable, while the authors of a crate are not.

So as of today, there is no way to automatically add authors on a new cargo project.

Upthrow answered 8/6, 2021 at 17:35 Comment(4)
So it is optional to add the authors field when publishing a project?Mcardle
If RFC 3052 is in production on crates.io, which it should, then yes.Upthrow
How would one go about updating "the book" to include this information (that the authors field is no longer automatically populated and no longer necessary)? I am just starting to read the book and found that confusing also (and I've been programming since 1972 and spent a day looking for how to set an environment variable or command line option or something to populate that field to my liking).Tiffinytiffy
File an issue with the repoUpthrow

© 2022 - 2024 — McMap. All rights reserved.