Rust 对于 WebAssembly的反对是最为欠缺的。毕竟 Mozilla 当初开发rust 是很大一部分为了编写Servo(浏览器渲染引擎)。

此外咱们介绍的wasmer和wasmtime两个wasm运行时,也是应用rust编写的。

对于应用rust编写wasm模块以及rust程序代码中应用wasm模块,这里不再具体讲述。之前的文章中,很多举例都是基于rust实现。

明天咱们次要看看rust对于wasi的反对。如果是纯内存运算,并不能体现出wasi的伟大意义--能够平安,高性能地与主机os交互(file, socket等)。

明天咱们一个简略的demo,演示一下。

1:创立rust我的项目

$ cargo new --bin wasi_hello_world

2: 编写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();}

代码非常简单,会在/helloworld门路下创立一个helloworld.txt文件, 而后写入Hello world!内容。

3:编译代码为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

当然如果你之前没有装置过wasm32-wasi target,须要首先执行:

rustup target add wasm32-wasi

编译胜利,在target/wasm32-wasi/release/ 目录下 生成 wasi_hello_world.wasm

4:运行

要授予应用Wasmtime CLI写入目录的性能,咱们须要应用--mapdir标记。 --mapdir容许咱们将客户机的虚构文件系统上的/helloworld目录映射到主机文件系统上的当前目录。

wasmtime --mapdir /helloworld::. target/wasm32-wasi/release/wasi_hello_world.wasmHello world!

同时,在当前目录生成了一个helloworld.txt文件。

总结

不过 目前来看,整个wasi都不够残缺成熟。包含对wasi反对最好的rust。

WASI仍在疾速成长,并且有许多我的项目正在利用WASI来做乏味的事件!

将来边缘计算以及serverless,都少不了wasi的身影。