diff --git a/wasi/Cargo.toml b/wasi/Cargo.toml new file mode 100644 index 0000000..5482da0 --- /dev/null +++ b/wasi/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "wasi" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/wasi/README.md b/wasi/README.md new file mode 100644 index 0000000..35990dd --- /dev/null +++ b/wasi/README.md @@ -0,0 +1,39 @@ + +```shell +rustup target add wasm32-wasi +``` + +```shell +cargo build --target=wasm32-wasi +``` + +```shell +$ ls target/ +debug wasm32-wasi +``` + + +运行时(Runtime) +运行这个文件,需要一个运行时(Runtime)。你也可以把这个理解成一个“容器”,“虚拟机”什么的,都行。但是准确的叫法是:运行时。 + +目前常见的运行时有 wasmtime,wasmer 等。 + +我们这次用 wasmtime 来运行吧。 + +`curl https://wasmtime.dev/install.sh -sSf | bash` + +or download from: + +https://github.com/bytecodealliance/wasmtime/releases + + +``` +$ wasmtime --dir=. target/wasm32-wasi/debug/wasi.wasm +Hello, world! +``` + + + +https://mp.weixin.qq.com/s/VYkUvD1NpRdx_qoIdCCq8w - _【Rust每周一知】Rust, wasm, wasi 试玩儿_ + + diff --git a/wasi/src/main.rs b/wasi/src/main.rs new file mode 100644 index 0000000..48808bb --- /dev/null +++ b/wasi/src/main.rs @@ -0,0 +1,10 @@ +use std::fs::File; +use std::io::prelude::*; + +fn main() -> std::io::Result<()> { + let mut file = File::create("foo.txt")?; + file.write_all(b"Hello, world!")?; + println!("Hello, world!"); + Ok(()) +} +