Rust for Windows (Subsystem for Linux)

This blog post explains how to install and use the Rust programming language under Windows and Windows Subsystem for Linux (WSL).

You can get the Windows Rust installer (rustup-init.exe) and instructions to install Rust on other operating systems including WSL here:

This is the installation command that I used under WSL (Ubuntu 20.04 LTS):

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Whether you run the Windows installer or the curl command, choose Option 1 to Proceed with installation.

After installing Rust on WSL, I received this error opening new shell windows and could not use the rust command line tools:

-bash: /home/jw/.bashrc: line 131: syntax error: unexpected end of file

The installer had neglected to insert linefeeds before appending “source $HOME/.cargo/env” to ~/.bashrc, so I added the linefeed before this line in that file. After starting a new shell, I could use rustup, rustc, and cargo.

Using the canonical main.rs:

fn main() {
    println!("Hello, world!");
}

To compile an “ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c875cbfbeac8280c8c04b5ff640114e1aa7a3cb8, for GNU/Linux 3.2.0, with debug_info, not stripped” under WSL:

rustc main.rs

To run it:

./main

To create a new project:

cargo new <ProjectName>
cd <ProjectName>
cargo run

To use the Windows variants and generate an .exe file, you can add .exe to the same commands that generate a “PE32+ executable (console) x86-64, for MS Windows”.

rustc.exe main.rs
main.exe

To create, compile, and run a new project:

cargo.exe new <ProjectName>
cd <ProjectName>
cargo.exe run

It seems that you can only use the Windows commands on Windows file systems.

thread rustc panicked trying to get session directory from IncrCompSession

2 thoughts on “Rust for Windows (Subsystem for Linux)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: