
How to Use the Command `rustup-init.sh` (with examples)
rustup-init.sh is a convenient script designed to install rustup, a toolchain installer for the Rust programming language, along with the Rust toolchain itself. Managing multiple versions and components of Rust is made easier with rustup, allowing developers to efficiently switch between toolchains depending on the requirements of their projects. This tool is particularly beneficial for those new to Rust or those working in environments with varying project prerequisites.
Use case 1: Download and run rustup-init to install rustup and the default Rust toolchain
Code:
curl https://sh.rustup.rs -sSf | sh -s
Motivation:
This command is the simplest way to get started with Rust. By piping the script directly from the web into a shell instance, it minimizes the number of steps necessary to install Rust, which is especially useful for those who want a quick setup without dealing with manual downloads or configurations.
Explanation:
curl https://sh.rustup.rs -sSf: This part of the command usescurlto download therustup-init.shscript from the official site.-s: Silent or quiet mode. It makescurlnot show progress or error messages.-S: When used with-s, it ensures curl will show errors if they occur.-f: Fail silently on server errors which meanscurlwill not output anything if it fails.
| sh -s: This pipes the downloaded script directly into the shell, where it is executed, with no additional arguments.
Example output:
Welcome to Rust!
This will download and install the official compiler for the Rust programming language.
Rust is distributed under an open-source license. This install is running in shell mode.
...
Rust is installed now. Great!
Use case 2: Download and run rustup-init and pass arguments to it
Code:
curl https://sh.rustup.rs -sSf | sh -s -- arguments
Motivation:
Sometimes, you need more control over the installation process, such as skipping confirmations or selecting a specific toolchain. This command allows you to append additional arguments to rustup-init, providing flexibility for tailored setups based on individual requirements or preferences.
Explanation:
sh -s -- arguments: The double dash--indicates the end of command options, allowing any following words to be recognized as arguments forrustup-inititself. You can replaceargumentswith valid options such as--default-toolchain,--profile, etc.
Example output:
Welcome to Rust!
Installation options were specified: -- <your-arguments>.
Proceeding with custom setup...
Installation complete.
Use case 3: Run rustup-init and specify additional components or targets to install
Code:
rustup-init.sh --target target --component component
Motivation:
Projects that involve cross-compilation or specialized features may require additional targets or components like rustfmt or clippy. This command allows the installation of these specific components or targets during the initial setup, thereby preparing your environment with everything needed right from the start.
Explanation:
--target target: Specifies a target to be installed that matches the development needs of your project, such as a specific architecture or operating system.--component component: Indicates additional tools or features needed, such asrustfmtfor code formatting orclippyfor linting.
Example output:
Installing additional target: target...
Installing component: component...
Installation complete. Ready to build your cross-platform application!
Use case 4: Run rustup-init and specify the default toolchain to install
Code:
rustup-init.sh --default-toolchain toolchain
Motivation:
This command is particularly useful for developers wanting a specific version of Rust that aligns with their project needs or a version known for particular stability or features. For instance, if you need Rust 1.56.0 due to compatibility requirements, this option allows for its direct installation as the default toolchain.
Explanation:
--default-toolchain toolchain: Sets the toolchain that rustup will install by default. Replacetoolchainwith your desired version of Rust, likestable,beta, or a specific version number.
Example output:
Specified default toolchain: toolchain
Downloading and installing specified toolchain...
Toolchain toolchain installed and configured as default.
Use case 5: Run rustup-init and do not install any toolchain
Code:
rustup-init.sh --default-toolchain none
Motivation:
There are scenarios where a user might want to set up rustup without installing any Rust toolchain initially. This could be because the user plans to install or manage toolchains manually later, possibly for testing different toolchains independently.
Explanation:
--default-toolchain none: This prevents the installation of any Rust toolchain during therustupinstallation, allowing for a later manual selection and setup of needed toolchains.
Example output:
rustup installation complete.
Default toolchain is set to none. No toolchains installed by default.
Use case 6: Run rustup-init and specify an installation profile
Code:
rustup-init.sh --profile minimal|default|complete
Motivation:
Not all developers need the complete Rust toolchain set up, so installation profiles offer a way to optimize for speed, space, or full capability. Depending on your needs, you can choose between minimal, default, or complete, which differ in size and components installed.
Explanation:
--profile minimal|default|complete: Allows selection of a predefined installation profile:minimal: Installs only the bare essentials.default: The most common choice with necessary components.complete: Includes all available components for maximum capability.
Example output:
Selected installation profile: minimal
Installing minimal set of toolchain components...
Installation complete with minimal profile.
Use case 7: Run rustup-init without asking for confirmation
Code:
rustup-init.sh -y
Motivation:
This command is useful for automated scripts or when you simply want to expedite the process by auto-confirming the installation steps. By running it in non-interactive mode, the installation proceeds without user input, streamlining the process and avoiding additional prompts.
Explanation:
-y: Automatically answers “yes” to all prompts, proceeding without any interactive confirmation, beneficial for automation scripts or bulk installations.
Example output:
auto-confirmation enabled with -y
Downloading and installing default components...
Rust toolchain installation completed successfully.
Conclusion:
The rustup-init.sh command script provides numerous options for customizing the installation of Rust and its toolchains. Whether you require specific versions, additional components, or just a minimal setup, these examples illustrate the flexibility and ease of setting up Rust environments tailored to diverse development needs. By understanding and leveraging these use cases, Rust developers can create efficient workflows and maintain flexible programming environments.


