How to set a cargo new template?

I notice that executing cargo new my_project_name --bin/lib will generate a .gitignore file, which contains only one line /target.

How can I configure cargo new so that the generated .gitignore file will contain one more line /.idea, since I use Clion+Rust plugin to write Rust codes.

Or generally speaking, is there a way to use templates to create projects? Thanks!

1 Like

There's no support for templates.

You can make yourself a global ignore file, e.g.:

git config --global core.excludesfile ~/.gitignore
echo .idea >> ~/.gitignore

I normally use cargo-generate when creating new projects. If you want, feel free to fork Michael-F-Bryan/github-template and customise it to your linking.

You can install cargo-generate from crates.io, which lets you build templates.

Thanks! But how to do it on Windows?

Thanks! But how can I create my own templates? I cannot find tutorials about how to customize templates in the repo of cargo generate, but only existing templates. Do you know such tutorials?

Does https://github.com/ashleygwilliams/cargo-generate/blob/master/README.md#templates explain? If not, filing an issue would be helpful Iā€™m sure.

Oh I can see how it works! Basically, there are only 4 fields in Cargo.toml we can customize as listed. As for other files, cargo generate just simply copy them into the generated project from the template.

1 Like

On Windows it's probably the same, except change the path from ~ to your actual home directory.

core.excludesfile has a default value ($XDG_CONFIG_HOME/git/ignore or $HOME/.config/git/ignore), so setting it explicitly is not necesarily needed. Although I have no idea what the default is on Windows.

I'd give %HOME%\.config\git\ignore a try...

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.