![654e518c17002631a3a09e995b74fb00.png](https://img-blog.csdnimg.cn/img_convert/654e518c17002631a3a09e995b74fb00.png)
在探讨 WASM 在服务端的巨大潜力时,我们提到 WASM 的一大优势就是支持有影响力的新锐编程语言,例如 Rust 。这篇文章将展示如何编写并执行一个 Rust + WebAssembly 程序,只有代码。
本文作者: Second State 的研究员、开源核心开发 Tim McCallum。
以下为正文:
该演示是使用 Ubuntu Linux 操作系统和 Google 的 Chrome 浏览器进行的。其他组合尚未经过测试。
![ef7ed3ca000b6837c1d81cc248beaadb.png](https://img-blog.csdnimg.cn/img_convert/ef7ed3ca000b6837c1d81cc248beaadb.png)
第1步:安装 Apache2 和 Rust
运行以下所有 Ubuntu 系统设置命令(更新,安装 Apache2 和 Rust )
sudo apt-get updatesudo apt-get -y upgradesudo apt-get -y install apache2sudo chown -R $USER:$USER /var/www/htmlcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource $HOME/.cargo/envrustup target add wasm32-wasirustup override set nightly
第2步:创建一个新的 Rust 项目
创建一个快速的 Rust 项目
cd ~cargo new --lib triplecd 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 ~/triplecargo build - releasecp -rp ~/triple/target/wasm32-wasi/release/triple_lib.wasm /var/www/html/triple.wasm
第7步:制作 HTML 网页
在 var / www / html / 目录中创建一个名为 Triple.html 的新文件,并使用以下代码填充它。
Rust to Wasm in under 5 minutes - Triple the number
Place a number in the box
Click the button
Triple the number
第8步:单击鼠标执行写好的 Rust 代码
在 triple HTML 页面:http://12.345.456.78/triple.html 上访问计算机的IP。
然后单击 “Triple the number” 按钮。
![2bf7e309bdcdc188dfd2127b1af7220b.png](https://img-blog.csdnimg.cn/img_convert/2bf7e309bdcdc188dfd2127b1af7220b.png)
将显示以下提示。
![186db8f7f41f44cef778c7b50b2ba7e5.png](https://img-blog.csdnimg.cn/img_convert/186db8f7f41f44cef778c7b50b2ba7e5.png)
如图所示:8的三倍等于24
到这里我们就完成了一个Rust + WebAssembly 程序,你也来试试吧!
作者:Tim McCallum
链接:https://juejin.im/post/5de62000e51d4557f852a141