It’s nice that cargo new creates a working program. At the same time it’s beyond primitive! Something I initially struggled with (and Rustlings doesn’t teach, despite a chapter on modules) is multi-file layout. So I suggest nudging newbies a little more with:
main.rs
// Make either `my_module.rs` or `my_module/mod.rs` be part of this compilation.
mod my_module;
// Import an identifier from a sub module.
use my_module::greeter::greet;
fn main() {
greet("world");
}
my_module/mod.rs
// Also make either `greeter.rs` or `greeter/mod.rs` be part of this compilation.
mod greeter;
// More mod statements and other content of this module can go here.
my_module/greeter.rs
pub fn greet(who: &str) {
// This macro can put a variable into a string.
println!("Hello, {who}!");
}
I used cargo new just a few hours ago to experiment with some code. All I had to do was to replace the main file with the example I was messing and to add a few dependencies. This new helpful cargo new might be useful for beginners doing their steps, but after that it would just add more cleanup burdens for such experiments.
There's cargo-generate that seem to be able to generate new projects according to some predefined template.
We've had many, many, many conversations about adding more detail to cargo new. And the general sentiment is that we shouldn't do anything that gets in the way of "delete the template code and start writing the code you want".
There are things we might wish to add that would not get in the way of that goal. But creating multiple sample source files would definitely get in the way.
Comprehensively demonstrating the use of desirable Rust features is the job of documentation, tutorials, and samples, not cargo new.
Sorry if this was redundant! The proposed similar posts were rather different.
I understand the sentiment. Where to draw the line is of course a matter of taste. In any modern file explorer (including the one in VScode and likely other IDEs) it takes few clicks to dump the directory. Hardly more effort than deleting unwanted lines. And how many projects do people create, that this would be painful?
As I pointed out, those coming from Rustlings would not know. (And I did start to open a few issues with things I saw my students struggle with – they got rejected. So I’m not hopeful to see that improve – especially in this case, as multi-file doesn’t well fit in there.)
If not to help newbies, what is the generated content good for? It’s really no effort either to type
Since, judging by messages, cargo new and cargo init seem to have the almost same functionality implemented separately, we could leave the latter unchanged. The former would then also allude to “newbie”. That would give real added value to having both commands.
I definitely have cargo generate on my list of things to explore. But it needs to be discovered and installed. That might just be too much of a hurdle to get it used by newbies. And from just a brief look, it seems to fall far short of the comfort Quarkus configurator offers.
In particular, it will auto-inherit any fields from the workspace. I brought up the idea of also copying over license files but iirc we instead want to look at improving how licenses are handled so it becomes part of the auto-inherit rather than us having heuristics.