只需 5 分钟,教你如何编写并执行一个 Rust + WebAssembly 程序


只需 5 分钟,教你如何编写并执行一个 Rust + WebAssembly 程序


在探讨 WASM 在服务端的巨大潜力时,我们提到 WASM 的一大优势就是支持有影响力的新锐编程语言,例如 Rust 。这篇文章将展示如何编写并执行一个 Rust + WebAssembly 程序,只有代码。

本文作者: Second State 的研究员、开源核心开发 Tim McCallum。

该演示是使用 Ubuntu Linux 操作系统和 Google 的 Chrome 浏览器进行的。其他组合尚未经过测试。


只需 5 分钟,教你如何编写并执行一个 Rust + WebAssembly 程序


第1步:安装 Apache2 和 Rust

运行以下所有 Ubuntu 系统设置命令(更新,安装 Apache2 和 Rust )

sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install apache2
sudo chown -R $USER:$USER /var/www/html
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
rustup target add wasm32-wasi
rustup override set nightly

第2步:创建一个新的 Rust 项目

创建一个快速的 Rust 项目

cd ~
cargo new --lib triple
cd triple

第3步:为 Wasm 配置 Rust

通过将下面的配置添加到 〜/ triple / Cargo.toml 文件的 lib 部分来配置 rust

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

第4步:指定构建目标

通过在 〜/ .cargo.config 中创建一个新文件并添加以下配置来完成 Rust 的配置

[build] 

target = "wasm32-wasi"

第5步:写 Rust

编写一个快速的 Rust 程序并将其保存为 Triple.rs(在 〜/ triple / src 目录中)

#[no_mangle]
pub extern fn triple(x: i32) -> i32 {
return 3 * x;
}

第6步:构建 Wasm 代码

将 Rust 代码构建到 Wasm 中,然后将 Wasm 文件复制到 Apache2 Web 服务器区域

cd ~/triple
cargo build - release
cp -rp ~/triple/target/wasm32-wasi/release/triple_lib.wasm /var/www/html/triple.wasm

第7步:制作 HTML 网页

在 var / www / html / 目录中创建一个名为 Triple.html 的新文件,并使用以下代码填充它。


\t
\t\t<link>
\t\t
\t
\t
\t\t

\t\t\t

\t\t\t

\t\t\t\tRust to Wasm in under 5 minutes - Triple the number
\t\t\t

\t\t\t

\t\t

\t\t

\t\t

\t\t\t

\t\t\t
Place a number in the box

\t\t\t
Click the button

\t\t\t

\t\t

\t\t

\t\t\t

\t\t\t

\t\t\t\t
\t\t\t\t

\t\t\t\t

\t\t\t\t\t<button>Triple the number/<button>
\t\t\t\t

\t\t\t\t

\t\t\t

\t\t
\t\t
\t

第8步:单击鼠标执行写好的 Rust 代码

在 triple HTML 页面:http://12.345.456.78/triple.html 上访问计算机的IP。

然后单击 “Triple the number” 按钮。


只需 5 分钟,教你如何编写并执行一个 Rust + WebAssembly 程序


将显示以下提示。


只需 5 分钟,教你如何编写并执行一个 Rust + WebAssembly 程序

如图所示:8的三倍等于24


到这里我们就完成了一个Rust + WebAssembly 程序,你也来试试吧!


作者:Tim McCallum
链接:https://juejin.im/post/5de62000e51d4557f852a141


分享到:


相關文章: