We’ll learn how to use GitHub Actions to learn Rust in your Web browser. We will write, build, test, run, and publish everything in one web page. The important thing is that no software is required!

Rust is one of today’s hottest programming languages. The geek gods love Rust. Rust has been voted the most popular programming language on Stackoverflow for four years in a row.

One of Rust’s most unique and popular features is its versatile compiler, which helps you ensure correctness and security before your program runs. As a result, Rust developers can write high-performance but secure programs. Rust eliminates some types of programming bugs, especially runtime bugs that are hard to correct.

If you haven’t tried writing with Rust, give it a try! Rust is like magic. I believe Rust could be the next Java or Ruby — a programming language that everyone will need to learn in the future.

However, learning Rust usually requires you to install a series of command-line tools on your computer. The Rust compiler is slow because the entire Rust paradigm is designed to drill down into source code and find bugs at compile time, not when they crash at run time.

Online Rust ides like Rust Playground and Repr. it are simple tools that don’t take full advantage of the Rust ecosystem of third-party compiler targets and libraries.

So you might be wondering, is it possible to learn Rust without installing the package?

With GitHub Actions, we can learn and explore Rust code directly in your browser.

Now with world-class CI/CD, Github Actions can easily automate all software workflows. We can build, test, and deploy code on GitHub. Do code reviews, branch management, and issue classification the way we want. The Hello World sample source code and workflow action used in this article can be found in the GitHub Repo.

Hello world

First, create a new GitHub repository O and add a Rust Source file. Let’s add a SRC /main.rs file with the following content:

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

Src/main.rs file

Next, return to the root/directory of the GitHub repository and add the Cargo. Toml file. This file describes how the Rust Cargo system should build and package our project.

[package]
name = "hello"
version = "0.1.0 from"
authors = ["ubuntu"]
edition = "2018"

[dependencies]
Copy the code

(Cargo. Toml file in the root of Repo)

Now that we have a complete Rust project, let’s get it up and running.

Github actions

On the GitHub Actions TAB, we can add the workflows associated with this project. These are actions that Github automatically performs when certain events occur, such as code pushes or commits. In our example, we want GitHub to automatically build and run main.rs and display the results.

The workflow Actions and their event triggers the.github/workflows directory are defined in the YML file under the properties. We can write our own YML file, or choose one from a ready-made template.

Here we select the Rust template, which GitHub allows us to edit the rust.yml file before checking it into Repo.

Default Rust operation template. You can edit as you like.)

Let’s take a minute here to explain how GitHub Actions work, the default rust.yml file shows:

  • Triggers whenever the user pushes code into or accepts PR into the Reporust.ymlWorkflow Action.
  • This workflow creates a virtual machine running the latest Ubuntu operating system. On this Ubuntu system, it will perform the following steps.
  • It will be taken frommasterBranch check out code.
  • It will runcargo build -- verboseCommand to compile and build Rust code.
  • It will runcargo test -- verboseCommand to execute the test case.
  • All standard output and console output from the above two commands on Ubuntu systems will be captured by GitHub Actions and displayed on the Web.

Edit the last line in rust.yml to execute cargo Run. Cargo Run runs the compiled binary program. We updated the rust.yml file as follows:

name: Rust

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs: build: runs-on: ubuntu-latest steps: -uses: actions/checkout@v2 -name: build run: cargo build - verbose - name: Run run: cargo runCopy the code

(We changed the last line of the default Rust template to cargo Run)

Now, whenever code is pushed to this Repo, the operation in rust.yml will be performed. You can see the results under the Actions TAB.

Click the results, and then click the Build TAB on the left to see the details. The build and run sections provide the most relevant details. The Run section shows a successful print of Hello World!

Next, you can add third-party dependencies in Cargo. Toml and build a complex Rust application in main.rs. As soon as someone enters the code, we can see the result.

Test Driven Development (TDD)

Of course, very few developers actually run programs to print text to the console. Cargo Run above is just a demo. In fact, most developers write functions and test cases for these functions. The most frequent task after compiling and building is to run test cases. Let’s see how the GitHub Action does it.

Create a new GitHub repo and add a SRC /lib.rs file below. As we can see, it defines a Rust function and some test cases. It can be built and distributed as a Rust library package.

pub fn say(s: &str) -> String {
let r = String::from("hello ");
return r + s;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn say_hello() {
let result = say("ssvm"); assert! (result.contains("hello ssvm")); }}Copy the code

(Src /lib.rs file)

Then, go back to the root/directory of GitHub Repo and add the following Cargo. Toml files.

[package]
name = "hello"
version = "0.1.0 from"
authors = ["ubuntu"]
edition = "2018"

[lib]
name = "hello_lib"
path = "src/lib.rs"
crate-type =["cdylib"]

[dependencies]
Copy the code

Click Actions and add the default Rust workflow. As mentioned above, the default Rust workflow ends with cargo Test, which is exactly what we need here.

Each time new code is pushed into this REPo, the workflow runs. You can click to open a successful build and see the output of the build and test actions.

What’s next

Now you can experiment with Rust code, build, test, and run it for free on GitHub, and use it for full console output, all in your browser!

resources

  • Learn to program with Rust

  • Learn more about GitHub Actions

  • Of course, the best way to run Rust on a server is in the WebAssembly virtual machine

  • Use the BUIDL IDE to deploy decentralized web applications on the public chain

About the author

Dr. Michael Yuan has written five books on software engineering. His latest book, Building Blockchain Apps, was published by Addison-Wesley in December 2019. Using the BUIDL IDE (which comes with tutorials in the book), developers can develop decentralized Web applications in less than five minutes.

Dr. Yuan is the CEO of Second State, a company focused on bringing WebAssembly and Rust technologies to cloud computing, blockchain and ARTIFICIAL intelligence applications.

Prior to that, Dr. Yuan was a long-term open source contributor to Red Hat, JBoss, and Mozilla.

In addition to being a software specialist, Dr. Yuan is an investigator at the National Institutes of Health Research Center and has received several research awards in cancer and public health research. He holds a Ph.D. in astrophysics from the University of Texas at Austin.