Search code examples
rustrust-cargo

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


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).


Solution

  • 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