Rust’s introduction to Hello world | webassembly

As wechat does not support external links, you can read the original at the end of the article for more resources

In the previous article, we introduced

Learn the frequently asked questions of webassembly

.

Now let’s start with rust.

Rust is the best language to write webassembly applications today.

For the source code repo used in this article, please click:

https://github.com/second-state/wasm-learning/tree/master/rust

Although webassembly supports many programming languages, rust has the best tools so far.

In the past four years, rust has been selected as the most popular programming language by stackoverflow users, and is one of the fastest growing programming languages.

Rust is as versatile and efficient as C, but because of its compiler design, rust is much safer than C.

Like C, rust has a learning curve.

In this tutorial, I’ll help you get started with the rust programming language.

You can use online resources, such as

This book

Learn more about the rust language.

Install rust

In a typical Linux system, run the following instructions, install the rust compiler and the cargo tool to create a management system.

  
    
  
  
  
$ sudo apt-get update
$ sudo apt-get -y upgrade

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ source $HOME/.cargo/env

Hello World

The source code of this demo application can be downloaded as follows:

https://github.com/second-state/wasm-learning/blob/master/rust/hello.md

First, let’s use

cargo

Create a new project.

  
    
  
  
  
$ cargo new hello
Created binary (application) `hello` package
$ cd hello

stay

src/ main.rs

In

main ()

Function, which is the entry point to execute the rust application.

Src/main. Rs

The contents of the document are as follows.

The code just prints a string “Hello world” to standard output.

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

Next, create a binary executable for your machine.

  
    
  
  
  
$ cargo build --release
Compiling hello v0.1.0 (/home/ubuntu/wasm-learning/rust/hello)
Finished release [optimized] target(s) in 0.22s

You can now run your first rust program and see “Hello world” on the console

You can now run your first rust program and see “Hello world” on the console

  
    
  
  
  
$ target/release/hello
Hello, world!

Interaction

The source code for this demo can be found here

https://github.com/second-state/wasm-learning/blob/master/rust/cli.md .

Again, let’s use cargo to create a new project.

  
    
  
  
  
$ cargo new cli
Created binary (application) `cli` package
$ cd cli

Src/ main.Rs

The contents of the document are as follows.

env::args()

Will save the string value that we pass from the command line when we execute the program.

Also note here that we first create a rust string and then attach more string references to it.

Why do we have to join string references instead of string values?That’s why rust makes programs safe. click

here

Learn more.

  
    
  
  
  
use std::env;

fn main() {
let args: Vec<String&> = env::args().collect();
println!("{}", String::from("Hello ") + &args[1]);
}

Next, create a QR code executable for your machine.

  
    
  
  
  
$ cargo build --release

You can run the program and pass in a piece of command line code

  
    
  
  
  
$ target/release/cli Rust
Hello Rust

What about webassembly?

Now we’ve learned how to create a local executable program from the rust source code.

Executable programs can only run on the build computer and may not be safe.

In the next tutorial, we will show you:

How to build webassembly bytecode program from rust source code instead of local executable file.

How to interact with webassembly program through web browser instead of tedious command line.

Related reading

String in webassembly

It only takes 5 minutes to teach you how to write and execute a rust + webassembly program

Wechat does not support external links. Click to read the original text to view the article resources

this article is from WeChat official account – WebAssembly Chinese community (webassemblywasm).
In case of infringement, please contact [email protected] Delete.
This article participates in the “OSC source creation program”. You are welcome to join and share.

Similar Posts: