How to use the command cargo yank (with examples)
This article will explain the various use cases of the cargo yank
command, which is used to remove a pushed crate from the index. It is typically used when there is a significantly broken crate that was accidentally released. This command prevents new projects from using the crate, but does not remove any data, meaning the crate is still present.
Use case 1: Yank the specified version of a crate
Code:
cargo yank crate@version
Motivation:
The motivation for yanking a specific version of a crate is to prevent new projects from using a specific version that is known to be broken or have issues. By yanking a specific version, you can ensure that new projects only use working and stable versions of the crate.
Explanation:
cargo yank
: The command used to yank a crate from the index.crate
: The name of the crate to yank.version
: The specific version of the crate to yank.
Example output:
info: yanked crate `crate` version `version`
Use case 2: Undo a yank (i.e. allow downloading it again)
Code:
cargo yank --undo crate@version
Motivation:
The motivation for undoing a yank is to allow downloading a crate that was previously yanked and prevent new projects from using it again. This is useful when a previously broken crate has been fixed and is ready to be used again.
Explanation:
cargo yank
: The command used to undo a yank.--undo
: An option that instructs the command to undo the yank.crate
: The name of the crate to undo the yank for.version
: The specific version of the crate to undo the yank for.
Example output:
info: restored crate `crate` version `version`
Use case 3: Specify the name of the registry to use
Code:
cargo yank --registry name crate@version
Motivation:
The motivation for specifying the name of the registry is to allow yanking or undoing a yank for a crate in a specific registry. This is useful when working with multiple registries or when a custom registry name is defined in the configuration.
Explanation:
cargo yank
: The command used to specify the registry name.--registry name
: An option that specifies the name of the registry to use.crate
: The name of the crate to yank or undo the yank for.version
: The specific version of the crate to yank or undo the yank for.
Example output:
info: yanked crate `crate` version `version` from registry `name`
Conclusion:
The cargo yank
command is a useful tool for managing crates in Rust projects. It allows you to yank specific versions of a crate to prevent new projects from using broken or unstable versions. You can also undo a yank to allow downloading a previously yanked crate again. Additionally, you can specify the name of the registry to use, which is useful when working with multiple registries or custom registry names.