How do I set the number of default parallel jobs [-j 4] for Cargo?

Viewed 361

Is there a way to set cargo to default add -j 4 to all the calls?

As workaround I've used this script:

cargoj4

#!/bin/bash
cargo "$1" -j 4

but this does not always work (I forget to call it :-\ or I have scripts in the project that use cargo and I cannot modify them).

2 Answers

You can create a config.toml to define parallel jobs. You need to create .cargo/config.toml. You can define it globally or per project, depending on your needs.

And then you just need to add

[build]
jobs = 4                      # number of parallel jobs, defaults to # of CPUs

You also have the possiblity of creating an alias for your command, which you would put in the same file:

[alias]
j4 = "build -j 4"

And then you can just invoke it as cargo j4

You can set this in cargo configuration file. You can create a config file in $HOME/.cargo/config.toml with the configuration you need.

[build]
jobs = 4 # number of parallel jobs, defaults to # of CPUs

Read more about Hierarchical structure, Configuration format and Jobs option here.

Related