使用WebAssembly(在Rust中创建)实现快速React组件
With the recent update to Rust, it is time to relook at using Rust and React together. I have a new found love for Rust and everything it can do as a language. One of its more impressive features is its ability to write WebAssembly(Wasm) without much overhead. I have explored how to use Rust to write Wasm before (Blog post coming soon!). But I wanted to see how easy it would be to integrate it into a standard React workflow.
随着最近 Rust的更新,是时候重新审视一下Rust和React的结合使用了。我对Rust和它作为一种语言所能做的一切有了新的爱。它的一个令人印象深刻的特点是它能够编写WebAssembly(Wasm)而没有太多的开销。我曾经探索过如何使用Rust来编写Wasm(博客文章即将发布!)。但我想看看把它整合到标准的React工作流程中会有多容易。
Introduction to Wasm
瓦斯姆简介
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
WebAssembly(缩写为Wasm)是一种基于堆栈的虚拟机的二进制指令格式。Wasm被设计为编程语言的可移植编译目标,能够在网络上部署客户端和服务器应用程序。
WebAssembly is a low-level assembly-like programming language that can run in most modern browsers. It has a compact binary format that gives us near-native performance on the web. As it becomes more popular, many languages have written bindings to compile into web assembly. It’s a tool that I have fallen for and am excited to share how we can use it in our React workflow.
WebAssembly是一种低级别的类似汇编的编程语言,可以在大多数现代浏览器中运行。它有一个紧凑的二进制格式,使我们在网络上有接近原生的性能。随着它变得越来越流行,许多语言都写了绑定程序来编译成网络汇编。这是一个让我倾心的工具,我很高兴能分享我们如何在React工作流程中使用它。
Developing a program from scratch in Wasm is not ideal, almost impossible. If you ever had the pleasure of coding in Assembly during university, you’d understand why. Luckily, there are some languages that can compile down to Wasm without much effort. This is possible in a large range of languages (C, Go, C#, Kotlin), but for this example we’ll be using Rust.
在Wasm中从头开发一个程序并不理想,几乎是不可能的。如果你在大学期间曾有过用汇编编码的乐趣,你会明白为什么。幸运的是,有一些语言可以不费吹灰之力编译成Wasm。这在很多语言中都可以实现(C、Go、C#、Kotlin),但在这个例子中我们将使用Rust。
use wasm_bindgen::prelude::*; #[wasm_bindgen]
extern { pub fn alert(s: &str);
} #[wasm_...