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

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 moduleconst helloWorld = await init("./pkg/wasm_hello_bg.wasm");// Call the Add function export from wasm, save the resultconst addResult = helloWorld.add(24, 24);// Set the result onto the bodydocument.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 moduleconst helloWorld = await init("./pkg/wasm_hello_bg.wasm");// Call the Add function export from wasm, save the resultconst addResult = helloWorld.add(24, 24);// Set the result onto the bodydocument.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.lryc.cn/news/469311.html

相关文章:

  • Visual studio 下载安装
  • jEasyUI 创建自定义视图
  • SpringMVC6-SpringMVC的视图
  • echarts给Y轴的不同轴线设置不同的颜色的样式
  • 从0到1构建 UniApp + Vue3 + TypeScript 移动端跨平台开源脚手架
  • 论文笔记:LaDe: The First Comprehensive Last-mile Delivery Dataset from Industry
  • 无用的知识又增加了-静态二值贝叶斯滤波
  • cesium相机(camera)控制
  • Java 反射
  • 【目标检测01】真实框、预测框、锚框和交并比IoU
  • 青少年编程能力等级测评CPA C++五级试卷(2)
  • SATA数据线
  • 《云原生安全攻防》-- K8s攻击案例:权限维持的攻击手法
  • 回溯算法-Java【力扣】【算法学习day.14】
  • 从本地到云端:跨用户请求问题的完美解决方案
  • leetcode day4 409+5
  • 英语语法学习框架(考研)
  • 基于neo4j的学术论文关系管理系统
  • C#中的委托、匿名方法、Lambda、Action和Func
  • IDEA关联Tomcat——最新版本IDEA 2024
  • 【如何获取股票数据18】Python、Java等多种主流语言实例演示获取股票行情api接口之沪深A股解禁限售数据获取实例演示及接口API说明文档
  • NVR小程序接入平台/设备EasyNVR多品牌NVR管理工具/设备的多维拓展与灵活应用
  • GPT-4o 和 GPT-4 Turbo 模型之间的对比
  • gin入门教程(10):实现jwt认证
  • Python 基础语法 - 数据类型
  • 自托管无代码数据库Undb
  • 正则的正向前瞻断言和负向前瞻断言
  • 大厂物联网(IoT)高频面试题及参考答案
  • react hook
  • Jetpack架构组件_LiveData组件