michelou / rust-examples

Playing with Rust on Windows

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Playing with Rust on Windows

Rust project This repository gathers Rust code examples coming from various websites and books.
It also includes several build scripts (batch files, Make scripts) for experimenting with Rust on a Windows machine.

Ada, Akka, C++, Dart, Deno, Docker, Flix, Golang, GraalVM, Haskell, Kafka, Kotlin, LLVM, Modula-2, Node.js, Scala 3, Spark, Spring, TruffleSqueak and WiX Toolset are other trending topics we are continuously monitoring.

Project dependencies

This project depends on two external software for the Microsoft Windows platform:

Optionally one may also install the following software:

Installation policy
When possible we install software from a Zip archive rather than via a Windows installer. In our case we defined C:\opt\ as the installation directory for optional software tools (similar to the /opt/ directory on Unix).

For instance our development environment looks as follows (March 2024) 3:

C:\opt\Git\             (367 MB)
C:\opt\msys64\          (3.4 GB)
C:\opt\VSCode\          (341 MB)
%USERPROFILE%\.cargo\   (100 MB)
%USERPROFILE%\.rustup\  (593 MB, installed toolchains and configuration options)

🔎 Git for Windows provides a BASH emulation used to run git from the command line (as well as over 250 Unix commands like awk, diff, file, grep, more, mv, rmdir, sed and wc).

Directory structure

This project is organized as follows:

bin\
docs\
examples\{README.md, diceroller, rust-ui-druid}
mastering-rust\{README.md, Chapter01, etc.}
possiblerust-examples\{README.md, InnerFunctions, TraitObjects, etc.}
rust-by-example\{README.md, 01_Display, etc.}
CONTRIBUTIONS.md
README.md
RESOURCES.md
SETUP.md
setenv.bat

where

We also define a virtual drive – e.g. drive R: – in our working environment in order to reduce/hide the real path of our project directory (see article "Windows command prompt limitation" from Microsoft Support).

🔎 We use the Windows external command subst to create virtual drives; for instance:

> subst R: %USERPROFILE%\workspace\rust-examples

In the next section we give a brief description of the batch files present in this project.

Batch commands

We distinguish different sets of batch commands:

  1. setenv.bat - This batch command makes the external tools such as cargo.exe, rustc.exe directly available from the command prompt.

    > setenv help
    Usage: setenv { <option> | <subcommand> }
     
      Options:
        -bash       start Git bash shell instead of Windows command prompt
        -debug      print commands executed by this script
        -verbose    print progress messages
     
      Subcommands:
        help        print this help message
  2. rust-by-example\*\build.bat - Finally each example can be built/run using the build command.

    While using cargo is the recommanded way to build/run Rust projects we also provide the batch command build.bat in order to exercise the usage of the rustc.exe command.

Usage examples

setenv.bat

We execute command setenv.bat once to setup our development environment; it makes external tools such as cargo.exe, git.exe and sh.exe directly available from the command prompt.

> setenv
Tool versions:
   cargo 1.75.0, rustc 1.75.0, rustfmt 1.7.-stable, rustup 1.26.0,
   pelook v1.73, make 4.4.1, git 2.43.0.windows.1, diff 3.10, bash 5.2.21(1)-release
 
> where cargo git sh
%USERPROFILE%\.cargo\bin\cargo.exe
C:\opt\Git\bin\git.exe
C:\opt\Git\mingw64\bin\git.exe
C:\opt\Git\bin\sh.exe
C:\opt\Git\usr\bin\sh.exe
C:\opt\msys64\usr\bin\sh.exe

Command setenv.bat with option -verbose displays additional information:

  • the tool paths (which may not contain the version suffix, i.e. C:\opt\Git\bin\git.exe in some installations),
  • the environment variables defined locally within this session,
  • and the path associations (i.e. F:\ in this case, but other drive names may be displayed as path associations are globally defined).
> setenv -verbose
Tool versions:
   cargo 1.73.0, rustc 1.73.0, rustfmt 1.6.0-stable, rustup 1.26.0,
   pelook v1.73, git 2.43.0.windows.1, diff 3.10, bash 5.2.15(1)-release
Tool paths:
   %USERPROFILE%\.cargo\bin\cargo.exe
   %USERPROFILE%\.cargo\bin\rustc.exe
   %USERPROFILE%\.cargo\bin\rustfmt.exe
   %USERPROFILE%\.cargo\bin\rustup.exe
   R:\bin\pelook.exe
   C:\opt\Git\bin\git.exe
   C:\opt\Git\mingw64\bin\git.exe
   C:\opt\Git\usr\bin\diff.exe
   C:\opt\Git\bin\bash.exe
Environment variables:
   "CARGO_HOME=%USERPROFILE%\.cargo"
   "GIT_HOME=C:\opt\Git"
   "MSYS_HOME=C:\opt\msys64"
   "RUSTUP_HOME=%USERPROFILE%\.rustup"
Path associations:
   R:\: => %USERPROFILE%\workspace-perso\rust-examples

Footnotes

[1] GNU Front-End for Rust

> c:\opt\msys64\usr\bin\pacman.exe -Sy mingw-w64-clang-x86_64-rust
:: Synchronizing package databases...
 clangarm64 is up to date
 mingw32 is up to date
 mingw64 is up to date
 ucrt64 is up to date
 clang32 is up to date
 clang64 is up to date
 msys is up to date
resolving dependencies...
looking for conflicting packages...
 
Packages (32) mingw-w64-clang-x86_64-brotli-1.1.0-1
        [...]
        mingw-w64-clang-x86_64-zlib-1.3-1
        mingw-w64-clang-x86_64-zstd-1.5.5-1
        mingw-w64-clang-x86_64-rust-1.75.0-1
 
Total Download Size:    255.01 MiB
Total Installed Size:  1655.04 MiB
 
:: Proceed with installation? [Y/n] y
[...]
 
> where /r c:\opt\msys64 rustc
c:\opt\msys64\clang64\bin\rustc.exe
 
> c:\opt\msys64\clang64\bin\\rustc.exe --version
rustc 1.75.0 (82e1608df 2023-12-21) (Rev1, Built by MSYS2 project)

[2] LLVM Backend

rustc.exe uses LLVM for code generation (see section Code Generation in the online Rustc Development Guide).
> %USERPROFILE%\.cargo\bin\rustc --version --verbose | findstr /b "rustc LLVM"
rustc 1.75.0 (82e1608df 2023-12-21)
LLVM version: 17.0.6
Rustc VersionLLVM Version
1.76.0
1.75.017.0.6
1.74.017.0.2
1.73.017.0.2
1.72.016.0.5
1.71.016.0.5
1.70.016.0.0
1.68.115.0.6
1.66.115.0.2
1.66.015.0.0
1.64.014.0.6
1.62.0 - 1.63.014.0.5
1.60.0 - 1.61.014.0.0
1.56.0 - 1.59.013.0.0
1.55.012.0.1
1.52.0 - 1.54.012.0.0
1.47.0 - 1.51.011.0.0

[3] Downloads

In our case we downloaded the following installation files (see section 1):
rust-init.exe                     ( 8 MB)
PortableGit-2.43.0-64-bit.7z.exe  (46 MB)
msys2-x86_64-20240113.exe         (86 MB)
Once the rustup.exe tool is installed, we can update our installation by simply running rustup.exe update.

mics/March 2024  

About

Playing with Rust on Windows


Languages

Language:Rust 100.0%