当前位置: 首页 > article >正文

WASM 使用说明23事(RUST实现)

文章目录

    • 1. wasm是什么
      • 1.1 chatgpt定义如下:
      • 1.2 wasm关键特性:
    • 2. wasm demo
      • 2.1 cargo 创建项目
      • 2.2 编写code
      • 2.3 安装wasm-pack
      • 2.4 编译
    • 3.1 html页面引用wasm代码(js引用)
    • 3.2 访问页面
    • 4 导入js function
      • 4.1 编写lib.rs文件,内容如下:
      • 4.2 编译
      • 4.3 html
      • 4.4 访问页面, 结果如下:

1. wasm是什么

1.1 chatgpt定义如下:

WebAssembly(WASM)是一种新兴的、低级的二进制格式,旨在为 web 提供一种高性能的执行环境。它可以与 JavaScript 互操作,使开发者能够在浏览器中运行复杂的应用程序,比如游戏、图形处理、科学计算等。

1.2 wasm关键特性:

  • 高性能:WASM 被设计为快速加载和执行,接近原生代码的性能。
  • 安全性:WASM 在一个安全的沙箱环境中运行,可以防止未授权的内存访问。
  • 跨平台:可以在任何支持的环境中运行,包括浏览器、服务器和嵌入式设备。
  • 语言支持:除了 JavaScript,许多编程语言(如
    Rust、C、C++、Go)可以编译为 WASM。

2. wasm demo

2.1 cargo 创建项目

cargo new --lib wasm-hello 

2.2 编写code

2.2.1 修改Cargo.toml文件, 内容如下:

[package]
name = "wasm-hello"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"

2.2.2 修改src/lib.rs文件, 内容如下:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

2.3 安装wasm-pack

cargo install wasm-pack

2.4 编译

进入项目根目录, 然后编译

wasm-pack build --target web

命令执行成功后, 截图如下:
说明: 1. wasm_hello_bg.wasm就是输出的二进制文件
在这里插入图片描述

3.1 html页面引用wasm代码(js引用)

wasm-hello根目录下创建index.html页面, 内容如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Hello World - Rust</title>
    <script type="module">
        import init from "./pkg/wasm_hello.js";
        const runWasm = async () => {
            // Instantiate our wasm module
            const helloWorld = await init("./pkg/wasm_hello_bg.wasm");

            // Call the Add function export from wasm, save the result
            const addResult = helloWorld.add(24, 24);

            // Set the result onto the body
            document.body.textContent = `Hello World! addResult: ${addResult}`;
        };
        runWasm();
    </script>
</head>
<body></body>
</html>

3.2 访问页面

截图如下:
在这里插入图片描述

4 导入js function

4.1 编写lib.rs文件,内容如下:

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
    return a + b;
}

// Here, we will define our external `console.log`
#[wasm_bindgen]
extern "C" {
    // Use `js_namespace` here to bind `console.log(..)` instead of just
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

// Export a function that will be called in JavaScript
// but call the "imported" console.log.
#[wasm_bindgen]
pub fn console_log_from_wasm() {
    log("This console.log is from wasm!");
}

4.2 编译

进入项目根目录, 然后编译

wasm-pack build --target web

4.3 html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Hello World - Rust</title>
    <script type="module">
        import init from "./pkg/wasm_hello.js";
        const runWasm = async () => {
            // Instantiate our wasm module
            const helloWorld = await init("./pkg/wasm_hello_bg.wasm");

            // Call the Add function export from wasm, save the result
            const addResult = helloWorld.add(24, 24);

            // Set the result onto the body
            document.body.textContent = `Hello World! addResult: ${addResult} `;

            helloWorld.console_log_from_wasm();
        };
        runWasm();
    </script>
</head>
<body></body>
</html>

4.4 访问页面, 结果如下:

页面有console输出“This console.log is from wasm!”
在这里插入图片描述


http://www.kler.cn/a/369042.html

相关文章:

  • 如何搭建直播美颜SDK平台的最佳实践?美颜API的实现与集成详解
  • 【Java并发编程】信号量Semaphore详解
  • CSS中为特定的元素设置背景图片(Top1,Top1,Top3)
  • Sentinel详解
  • STM32 第17章 EXIT--外部中断/事件控制器
  • Java | Leetcode Java题解之第513题找树左下角的值
  • 【TIMM库】是一个专门为PyTorch用户设计的图像模型库 python库
  • 15分钟学 Go 第 23 天:并发基础:Goroutines
  • 【CSS3】css开篇基础(4)
  • JavaScript 函数与事件处理
  • 灵动AI:艺术与科技的融合
  • 网络搜索引擎Shodan(4)
  • 最优化方法-无约束优化算法(最速下降法)matlab实现
  • opencv学习笔记(3):图像和视频的读取(C++)
  • 【AIGC】ChatGPT提示词Prompt精确控制指南:Scott Guthrie的建议详解与普通用户实践解析
  • 26.Redis主从架构
  • Hadoop-001-本地虚拟机环境搭建
  • oracle 行转列(PIVOT 多个行数据按照指定的列进行汇总) 列转行(UNPIVOT)
  • RHCE笔记-NFS服务
  • 第十七周:机器学习
  • 2025年软考高级哪个最简单?
  • 重构商业生态:DApp创新玩法与盈利模式的深度剖析
  • c语言中整数在内存中的存储
  • 7、基于爬虫+Flask+Echarts+MySQL的网易云评论可视化大屏
  • 【MySQL】提高篇—数据完整性与约束:主键、外键、唯一约束和检查约束的概念
  • Linux 命令行查看当前目录的总大小/总磁盘空间/磁盘清理