การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`
เชี่ยวชาญการใช้ `wasm-bindgen` เพื่อแลกเปลี่ยนโครงสร้างข้อมูลซับซ้อนและเชื่อมการทำงานระหว่าง Rust WASM กับ JavaScript ได้อย่างราบรื่น
การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen` เป็นบทเรียน WebAssembly (WASM) for High Performance Apps ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebAssembly (WASM) for High Performance Apps และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Unlocking Complex Interop
Welcome to the final lesson on building high-performance WASM with Rust! So far, you've learned to set up Rust for WASM and write basic functions.
But what about exchanging complex data like strings, objects, or arrays? This is where wasm-bindgen comes in. It's a Rust tool that simplifies interaction between Rust and JavaScript.
What is `wasm-bindgen`?
wasm-bindgen is a utility that helps you create JavaScript bindings for Rust WebAssembly modules. It handles the low-level details of passing data types between the two languages.
- Automates FFI: It automatically generates JavaScript glue code and Rust FFI (Foreign Function Interface) declarations.
- Rich Type Support: It allows you to pass more complex types like
String, JavaScript objects, and even DOM elements directly. - Improved Developer Experience: It makes your Rust WASM modules feel more like native JavaScript modules.
Setting Up for `wasm-bindgen`
To use wasm-bindgen, you need to add it as a dependency in your Cargo.toml. You'll also use the wasm-pack tool, which leverages wasm-bindgen to build your WASM module.
Add this to your Cargo.toml:
[dependencies]
wasm-bindgen = "0.2"Then, mark your functions with #[wasm_bindgen].
Exporting Basic Functions
Let's start with a simple function. The #[wasm_bindgen] attribute makes a Rust function available to JavaScript.
Try running this example. The add_numbers function is exposed, and run_add_example demonstrates calling it.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add_numbers(a: u32, b: u32) -> u32 {
a + b
}
// This function serves as a runnable entry point for demonstration.
#[wasm_bindgen]
pub fn run_add_example() -> u32 {
add_numbers(10, 5)
}Passing Strings to Rust
One of wasm-bindgen's great features is how it handles strings. You can pass a JavaScript string directly to a Rust function expecting &str or String.
Here, the greet function takes a string argument from JavaScript.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
#[wasm_bindgen]
pub fn run_greet_example() -> String {
greet("CoddyKit User")
}Returning Strings from Rust
Similarly, Rust functions returning a String will automatically be converted into a JavaScript string. This makes working with text across the boundary very natural.
The get_message function returns a Rust String.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_message() -> String {
String::from("This message came from Rust!")
}
#[wasm_bindgen]
pub fn run_get_message_example() -> String {
get_message()
}Exporting Structs as JS Classes
wasm-bindgen can also expose Rust structs as JavaScript classes. This allows you to create and manipulate Rust objects directly from JavaScript.
The User struct below becomes a class in JavaScript, complete with a constructor.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct User {
name: String,
age: u8,
}
#[wasm_bindgen]
impl User {
#[wasm_bindgen(constructor)]
pub fn new(name: String, age: u8) -> User {
User { name, age }
}
}
#[wasm_bindgen]
pub fn create_user_example() -> User {
User::new("Alice".to_string(), 30)
}Adding Methods to JS Classes
Once a Rust struct is exposed as a JavaScript class, you can add methods to its impl block. These methods will be callable directly on the JavaScript object.
Let's add a greet method to our User struct.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub struct User {
name: String,
age: u8,
}
#[wasm_bindgen]
impl User {
#[wasm_bindgen(constructor)]
pub fn new(name: String, age: u8) -> User {
User { name, age }
}
pub fn greet(&self) -> String {
format!("Hi, my name is {} and I'm {} years old.", self.name, self.age)
}
}
#[wasm_bindgen]
pub fn run_user_greet_example() -> String {
let user = User::new("Bob".to_string(), 25);
user.greet()
}Passing Rust Vectors to JS
wasm-bindgen can also handle basic collection types like Vec for primitive types. It will convert a Rust Vec into a JavaScript array.
This allows you to pass lists of numbers or strings easily.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn process_numbers(numbers: Vec<u32>) -> Vec<u32> {
numbers.iter().map(|n| n * 2).collect()
}
#[wasm_bindgen]
pub fn run_process_numbers_example() -> Vec<u32> {
let input = vec![1, 2, 3];
process_numbers(input)
}`wasm-bindgen` Interop Check
Which of the following statements about wasm-bindgen are TRUE?
Recap: Mastering Interop
You've now mastered wasm-bindgen, a powerful tool for seamless interaction between Rust and JavaScript in your WebAssembly projects.
- It automates binding generation, making your Rust functions and types easily accessible from JS.
- It enables the exchange of complex data types like strings, structs (as JS classes), and vectors.
- This significantly improves developer experience, letting you write high-performance Rust logic that feels native in a JavaScript environment.
With wasm-bindgen, your Rust WASM modules can become integral, high-performance components of any web application!
คำถามที่พบบ่อย
บทเรียน “การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebAssembly (WASM) for High Performance Apps ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebAssembly (WASM) for High Performance Apps มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`”
เชี่ยวชาญการใช้ `wasm-bindgen` เพื่อแลกเปลี่ยนโครงสร้างข้อมูลซับซ้อนและเชื่อมการทำงานระหว่าง Rust WASM กับ JavaScript ได้อย่างราบรื่น คุณปฏิบัติ WebAssembly (WASM) for High Performance Apps ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebAssembly (WASM) for High Performance Apps หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebAssembly (WASM) for High Performance Apps บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebAssembly (WASM) for High Performance Apps นี้ได้ไหม
ได้ บทเรียน WebAssembly (WASM) for High Performance Apps ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การตั้งค่า Rust สำหรับ WebAssembly
- การเขียนฟังก์ชัน Rust สำหรับ WASM
- การทำงานร่วมกับ JS อย่างมีประสิทธิภาพด้วย `wasm-bindgen`
- การจัดการข้อผิดพลาดข้ามขอบเขต WASM