I decided a while ago that the next language I would try to learn is Rust. I don’t want to go too much into Rust other than to repeat its own summary:

Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. – https://www.rust-lang.org/

But like all things, there are bits that need to be installed. I want to get started, I don’t want to muck around with installers.

Online playground

The Rust docs online include Rust By Example, which is interactive documentation that you can edit and execute from inside the browser.

There is also the Rust Playground which lets you write, compile, run, decompile Rust, and even generate intermediate representations of it in several formats.

Many modern languages now have playgrounds like this including C# (.Net Fiddle or C# Pad), JavaScript (JS Bin, JS Fiddle or The Javascript Playground) and F# (Try F#) (as well as many others).

Ok for playing around, but not sufficient, especially if you want to write custom IP, or start doing IO for instance.

Why not docker?

On windows, I can build and run Rust on Docker, without installing Rust. Since I already have Docker installed, this is the fastest way to get going.

docker run -it -v "$(pwd):/usr/src/myapp" -w /usr/src/myapp --rm rust:latest cargo test

This PowerShell-compatible command launches and tests my application in docker. Breaking it down:

  • docker run says we are executing a container from an image
  • -it specifies we want it to be an interactive session (show me the console logs)
  • -v "$(pwd):/usr/src/myapp" I want to map (share) my current local folder inside the container at the path “/usr/src/myapp”
  • -w /usr/src/myapp start the session inside the working directory as the one we just mapped from our local system
  • --rm clean up the instance when we exit
  • rust:latest use the latest rust container image
  • cargo test run the rust command cargo test (a bit like dotnet test)

An Example

Let’s work this a step at a time. I want to:

  • Create a new rust project
  • Build it

We had a look at the rust command to build in detail, so we can now quickly look at the commands to create a new rust project and then build it.

File New Project

In Rust, there is a tool called Cargo, which works a lot like dotnet for C#. There is also a compiler rustc that will compile a single file. You could just run the above command with rustc main.rs instead of cargo test and it will build the main.rs file from inside the container.

Instead, we will use cargo.

(As a caveat, we need to declare a $USER environment variable for cargo new since this appears to be unset on this Docker container image.)

mkdir rustroot
cd rustroot
docker run -e USER=MyUser -it -v "$(pwd):/usr/src" -w /usr/src --rm rust:latest cargo new --bin myApp

If everything went well, you should now have a local file structure on your machine:

+-- myApp
    |-- Cargo.toml
    +-- src
        \-- main.rs

Let’s move into the newly created directory.

cd myApp

build and test

From inside the myApp folder, we can once again launch a docker instance to perform our build and test:

docker run -it -v "$(pwd):/usr/src/myapp" -w /usr/src/myapp --rm rust:latest cargo test

You should now see the following successful output:

> docker run -it -v "$(pwd):/usr/src/myapp" -w /usr/src/myapp --rm rust:latest cargo test
   Compiling myApp v0.1.0 (file:///usr/src/myapp)
warning: crate `myApp` should have a snake case name such as `my_app`
  = note: #[warn(non_snake_case)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 1.4 secs
     Running target/debug/deps/myApp-f7629208065f5316

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

At some point, I had an error trying to run this a second time. I used rm -r ./target to fix it, possibly because of some sort of locking? I’m not sure. All I knew at the time was that if I didn’t remove it, it didn’t build a second time. It seems to work fine now though. shrug

The End

Long term I will still want to install a development environment for Rust, but to get going fast and for just learning the syntax and libraries, this is a nice quick way to get started. (And if you want to run a Rust workshop/lesson, it may be easier to get certain groups up and running if you know they already have docker - like your team at work, perhaps.)