Search code examples
rustrust-cargo

Can inherited dependencies in rust be aliased in the Cargo.toml file?


I have a workspace and one of my crates has this :

[dependencies]
regex = { workspace = true, features = ["unicode"] }

but I'd like to perhaps use an alias

[dependencies]
rgx = { package = "regex", workspace = true, features = ["unicode"] }

This is not a possibility so is there an alternative way of aliasing in the toml file itself?


Solution

  • No, if you want an alias, then it has to be at the workspace level. See the cargo book

    Along with the workspace key, dependencies can also include these keys:

    • optional: Note that the [workspace.dependencies] table is not allowed to specify optional.
    • features: These are additive with the features declared in the [workspace.dependencies]

    Other than optional and features, inherited dependencies cannot use any other dependency key [..]

    This works though:
    Cargo.toml

    [workpace]
    members = ["sub"]
    
    [workspace.dependencies]
    rnd = { package = "rand", version = "*" }
    rand = { version = "*" }
    

    sub/Cargo.toml

    #…
    [dependencies]
    rnd = { workspace = true }
    # or if you don't want it renamed in this package
    #rand = { workspace = true }
    # you cannot use both at the same time though.