iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦾

Development Environment: Setting up Rust (MSVC) on Windows (2023 Edition)

に公開

tl;dr

  • To develop in Rust[1], you need to install Visual Studio Build Tools[2].
  • Set up environment variables and install Rust based on those settings.
  • These steps can be performed efficiently using the winget command.

Introduction

To use the Rust programming language, a C++ compiler is required for compiling and building source code.
Rust for Windows requires either Visual C++ or GNU C++.
Therefore, you must set up a C++ development environment before installing Rust.

In this article, we will pre-install Visual Studio Build Tools, a software development tool from Microsoft that includes the Visual C++ compiler.

Installing Rust and Visual Studio Build Tools can be done easily using the winget[3] command.
winget is the official package manager for Windows, allowing you to install software in bulk from the command line.
This article provides a step-by-step explanation of how to install Rust and Visual Studio Build Tools.

1. Installing Visual Studio Build Tools

Rust requires a C++ compiler to function properly. To use Rust, we will pre-install Visual Studio Build Tools, Microsoft's software development tool suite that includes the C++ compiler.

For more details, please refer to the "Visual Studio Build Tools Installation Procedure Guide".

1.1. Installing Visual Studio Build Tools

Follow these steps to install Visual Studio Build Tools.

  1. Launching Visual Studio Installer:
    Run the following command to launch the Visual Studio Installer.

    winget install Microsoft.VisualStudio.2022.BuildTools
    
  2. Selecting Components:
    On the [Select Components] screen, select [Desktop development with C++].
    Installer: Select Components

  3. Installing Build Tools:
    Click the [Install] button to install the selected components.
    Install

  4. Closing Visual Studio Installer:
    Click the [×] in the upper right corner to exit the installer.

This completes the installation of Visual Studio Build Tools.
Next, we will configure the Path for the Visual Studio Build Tools.

1.2. Setting the Path

Follow these steps to set the Path.

  1. Open the [System Properties] dialog:
    Run the following command.

    systempropertiesadvanced.exe
    

    The [System Properties] dialog will be displayed.
    System Properties

  2. Open the [Environment Variables] dialog:
    Click the [Environment Variables] button. The [Environment Variables] dialog will be displayed.
    Environment Variables

  3. Edit the Path in [System variables]:
    Select Path under "System variables" and click [Edit(I)].
    The [Edit System Variable] dialog will be displayed.
    Editing Path

  4. Add the C++ compiler path:

    Click [New] and add the following Path.

    Path Overview
    C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.36.32532/bin/HostX64/x64 C++ compiler
  5. Closing the dialogs:
    Click [OK] and close all dialogs.

  6. Restart the PC:
    Restart the PC to reflect the configured Path in the environment.

This completes the setup for Visual Studio Build Tools.

2. Installing Rust

2.1. Setting Environment Variables

Below are the environment variables related to the installation and operation of Rust. By setting these variables before installation, you can specify the installation destination and other settings.
Below are the environment variables used by Rust and their settings.

Environment Variable Overview Remarks
CARGO_HOME Installation destination for Cargo Installation destination for the Rust package manager Cargo. The Rust compiler and other tools will also be stored under this directory.
Path Rust tool path Adds the Path for executing various Rust tools.
RUSTUP_HOME Storage for various Rust data Stores various data such as the Rust toolchain, the compiler itself, and C headers and libraries.

Configuring Environment Variables

Follow these steps to set the environment variables.

  1. Open the [System Properties] dialog:
    Run the following command.

    systempropertiesadvanced.exe
    

    The [System Properties] dialog will be displayed.
    System Properties

  2. Open the [Environment Variables] dialog:
    Click the [Environment Variables] button. The [Environment Variables] dialog will be displayed.
    Environment Variables

  3. Editing [System variables]:
    Set the following environment variables in "System variables".

    Environment Variable Setting Remarks
    CARGO_HOME c:\lang\rust In my environment, various languages are stored under c:\lang.
    Path %CARGO_HOME%\bin Add the setting on the left to the Path.
  4. Editing [User variables]:
    Set the following environment variables in "User variables".

    Environment Variable Setting Remarks
    RUSTUP_HOME %XDG_DATA_HOME%\rustup Corresponds to the XDG Base Directory[4].
  5. Closing the dialogs:
    Click [OK] on all dialogs to close them.

  6. Restart the PC:
    Restart the PC to reflect the configured Path throughout the environment.

Environment variable setup is now complete.

2.2. Installing Rust

When installing Rust from the official website, Rustup[5], the installer for the Rust toolchain, is used.
Rustup also installs cargo and other toolchains necessary for software development in Rust.
It is a tool that allows you to manage, build, and update the entire Rust development environment.

winget packages include the Rust language and its installer Rustup.
This time, we will install the rustup package to use Rustup.

Below is a summary of the Rust language-related packages within winget.

Package ID Description Remarks
Rustlang.Rustup The Rust toolchain installer The installer for Rustup. Rust is automatically installed by the installed rustup.
Rustlang.Rust.MSVC Rust (MSVC) The version of the Rust language that uses Visual C++ for C/C++ modules.
Rustlang.Rust.GNU Rust (GNU) The version of the Rust language that uses GNU C++ for C/C++ modules.

Follow these steps to install Rust.

  1. Installing rustup:
    Run the following command in Windows Terminal to install Rustup.

    winget install rustlang.rustup
    
  2. Installing Rust:
    rustup will run automatically and install rust.

    rustup
    info: profile set to 'default'
    info: default host triple is x86_64-pc-windows-msvc
    info: syncing channel updates for 'stable-x86_64-pc-windows-msvc'
     .
     .
     ,
    

This completes the installation of Rust.

2.3. Checking Rust Operation

If the installation completed successfully, the Rust compiler rustc should be available.
Follow these steps to check the operation of rustc.

  1. rustc version:
    Run the following command in Windows Terminal.

    rustc --version
    
  2. Checking the version:
    Check if a version number like the following is displayed in the Terminal.

    rustc 1.71.0 (8ede3aae2 2023-07-12)
    

If a version number like (rustc 1.71.0 ...) is displayed, Rust has been successfully installed.

3. Verifying the Development Environment

To confirm that Rust has been installed correctly and that you can program with Rust, we will create a simple program that outputs Hello, World.

3.1. Hello, World in Rust

Follow these steps to verify your Rust development environment.

  1. Navigate to the working directory:
    Open PowerShell and navigate to a suitable working directory for programming.

    cd ~/workspacies/temp/rust
    
  2. Creating hello.rs:
    Create a hello.rs file and write the following Rust code into it.

    // hello.rs
     fn main()
     {
        println!("Hello, rust World!")
    }
    
  3. Creating hello.exe:
    Compile hello.rs to create hello.exe.

     rustc hello.rs
    
  4. Running hello.exe:
    Run hello.exe and check if it works correctly.

    Hello, rust World!
    

If "Hello, rust World!" is output as shown above, Rust is working correctly.

Conclusion

By following the steps above, you can install the MSVC version of Rust in a Windows environment.
In summary, Rust can be installed through the following major steps:

  1. Before installing Rust, install Visual Studio Build Tools, which includes the C++ compiler.
  2. By setting environment variables in advance, you can freely change the installation destination of Rust.
  3. Use the winget command to easily install Rust.

By performing these steps, you can smoothly build a Rust development environment.

Please use this article as a reference to set up your Rust development environment. Then, leverage your newly acquired Rust skills to further expand your world of engineering.

Happy hacking!

References

Websites

脚注
  1. Rust: A system programming language that is fast, safe, and combines concurrency and practicality. It is characterized by a design that achieves both memory safety and high performance. ↩︎

  2. Visual Studio Build Tools: A software development tool for cross-platform development provided by Microsoft. This is required for installing Rust. ↩︎

  3. winget: The official Windows package manager. ↩︎

  4. XDG Base Directory: A standard directory specification used for storing configuration files in Linux/UNIX. ↩︎

  5. Rustup: The official tool for building and managing the Rust development environment. Using rustup is recommended when setting up a Rust language development environment. ↩︎

GitHubで編集を提案

Discussion