Rust’s support for webassembly is the most perfect. After all, a large part of Mozilla’s original development of trust was to write servo (browser rendering engine).
In addition, the two wasm runtimes, wasmer and wasmtime, which we introduced, are also written with rust.
For the use of rust to write wasm module and the use of wasm module in the code of rust program, we will not talk about it in detail here. In previous articles, many examples are based on the implementation of rust.
Today we mainly look at the support of rust for Wasi. If it is a pure memory operation, it can not reflect the great significance of Wasi — it can interact with the host OS (file, socket, etc.) safely and with high performance.
Today we’re going to do a simple demo.
1: Create a trust project
$ cargo new --bin wasi_hello_world
2: Writing main.rs
use std::io::prelude::*;
use std::fs;
fn main() {
println!("Hello world!");
let mut file = fs::File::create("/helloworld/helloworld.txt").unwrap();
write!(file, "Hello world!n").unwrap();
}
The code is very simple and will be displayed in the/helloworld
Create one under the pathhelloworld.txt
File, and then write itHello world!
Content.
3: The compiled code is wasm
cargo build --release --target wasm32-wasi
Compiling wasi_hello_world v0.1.0 (/Users/iyacontrol/rust/wasi_hello_world)
Finished release [optimized] target(s) in 1.63s
Of course, if you have not installed wasm32 Wasi target before, you need to first perform the following steps:
rustup target add wasm32-wasi
Compiled successfully, intarget/wasm32-wasi/release/
Generated under directorywasi_hello_world.wasm
。
4: Operation
To grant the ability to write to a directory using wasmtime cli, we need to use the — mapdir flag. –Mapdir allows us to map the / HelloWorld directory on the client’s virtual file system to the current directory on the host’s file system.
wasmtime --mapdir /helloworld::. target/wasm32-wasi/release/wasi_hello_world.wasm
Hello world!
At the same time, ahelloworld.txt
Documents.
summary
But at present, the whole Wasi is not mature enough. Including the best rust for Wasi.
Wasi is still growing rapidly, and many projects are using Wasi to do interesting things!
In the future, both edge computing and serverless will need Wasi.