How to use the command 'rustup target' (with examples)
This command allows you to modify a toolchain’s supported targets in Rust. You can add or remove targets, as well as list available and installed targets for a toolchain.
Use case 1: Add a target to a toolchain
Code:
rustup target add --toolchain toolchain target
Motivation: The rustup target add
command is used to add a new target to a specific toolchain. This is useful when you need to build your Rust code for a specific platform or architecture. By adding a new target, you can cross-compile your code to run on different devices or platforms.
Explanation:
--toolchain toolchain
: This option allows you to specify the toolchain to which you want to add the target. You can provide the name of the toolchain (e.g.,stable
,nightly
) or an explicit version (e.g.,1.50.0
).target
: This argument represents the target to be added. It can be a target triple, such asx86_64-unknown-linux-gnu
orarmv7-unknown-linux-gnueabihf
.
Example output:
info: downloading component 'rust-std' for 'x86_64-unknown-linux-gnu'
info: installing component 'rust-std' for 'x86_64-unknown-linux-gnu'
Use case 2: Remove a target from a toolchain
Code:
rustup target remove --toolchain toolchain target
Motivation: The rustup target remove
command is used to remove a target from a specific toolchain. If you no longer need to build your Rust code for a specific platform or architecture, you can remove the target to free up disk space and simplify your toolchain configurations.
Explanation:
--toolchain toolchain
: This option allows you to specify the toolchain from which you want to remove the target. It can be the name of the toolchain (e.g.,stable
,nightly
) or an explicit version (e.g.,1.50.0
).target
: This argument represents the target to be removed. It should be a target triple that was previously installed.
Example output:
info: removing component 'rust-std' for 'x86_64-unknown-linux-gnu'
info: rolling back changes done to 'x86_64-unknown-linux-gnu'
Use case 3: List available and installed targets for a toolchain
Code:
rustup target list --toolchain toolchain
Motivation: The rustup target list
command allows you to see a list of all available and installed targets for a specific toolchain. This is useful when you need to know which platforms are supported by your toolchain or want to check if a particular target is already installed.
Explanation:
--toolchain toolchain
: This option specifies the toolchain for which you want to list the targets. It can be the name of the toolchain (e.g.,stable
,nightly
) or an explicit version (e.g.,1.50.0
).
Example output:
x86_64-apple-darwin
x86_64-pc-windows-msvc
x86_64-unknown-linux-gnu
Use case 4: List installed targets for a toolchain
Code:
rustup target list --toolchain toolchain --installed
Motivation: The rustup target list
command with the --installed
option allows you to see only the targets that are currently installed for a specific toolchain. This is useful when you want to check the installed targets without seeing the full list of available targets.
Explanation:
--toolchain toolchain
: This option specifies the toolchain for which you want to list the installed targets. It can be the name of the toolchain (e.g.,stable
,nightly
) or an explicit version (e.g.,1.50.0
).--installed
: This option filters the list to show only the installed targets.
Example output:
x86_64-unknown-linux-gnu
Conclusion:
The rustup target
command is a powerful tool for managing the supported targets in your Rust toolchain. With it, you can add or remove targets, as well as list the available and installed targets for a specific toolchain. Using these commands, you can easily customize your toolchain to suit your development needs and build your Rust code for different platforms or architectures.