A Rusty hello world
Getting started with the Rust programming language - Rust is the best-supported language on WebAssembly today.
This page is outdated. Please visit here to see a Rusty hello world.
While WebAssembly supports many programming languages, Rust by far has the best tooling. Rust is voted the most beloved programming language by StackOverflow users for the past 4 years in a row. It is one of the fastest-growing programming languages.
Rust is versatile and performant like C, but much safer than C due to its compiler design. Like C, it has a bit of a learning curve. In this tutorial, I will get you started with the Rust programming language. From here, you can learn much more about the Rust language through online resources like the book.
Install Rust
On a typical Linux system, run the following commands to install Rust compiler and the cargo
tool for build management.
Hello world
The source code of the tutorial is here.
First, let’s create a new project using cargo
.
The main()
function in the src/main.rs
file is the entry point when we execute the Rust application. The src/main.rs
file content is as follows. The code just prints a string “hello world” to the standard output.
Next, build the binary executable file for your machine.
You can now run your first Rust program and see “Hello World!” on the console.
Interactive greeting
The source code of the tutorial is here.
Again, let’s create a new project using cargo
.
The content of the src/main.rs
file is as follows. The env::args()
holds the string values passed from the command line when we execute the program. Here you can also see that we first create a Rust string, and then append more string references to it. Why do we have to concatenate string references instead of string values? Well, that is how Rust makes programs safe. You can read more here.
Next, build the binary executable file for your machine.
You can run the program and pass in a command-line argument.
What about WebAssembly?
Now we have seen how to create build native executable programs from Rust source code. The executable program can only run on your build machine and could be unsafe. In the next tutorial, I will show you.
How to build WebAssembly bytecode programs instead of native executable files from Rust source code.
How to interact with WebAssembly programs via a web browser instead of the cumbersome command line.
Stay tuned!
Last updated