كتابة دوال Rust لـWASM
طوّر دوال Rust يمكن ترجمتها إلى WASM وإتاحتها لبيئة JavaScript لتنفيذها
كتابة دوال Rust لـWASM درس مجاني في WebAssembly (WASM) for High Performance Apps على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في WebAssembly (WASM) for High Performance Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة WebAssembly (WASM) for High Performance Apps 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Unlocking Rust for the Web
Welcome back! In the previous lesson, we set up our Rust environment for WebAssembly. Now, let's dive into writing Rust functions that can be used directly in your web applications.
The goal is to expose Rust's performance and safety to JavaScript, making your web apps even more powerful.
Standard Rust Function Syntax
Before exporting, let's quickly recall how standard Rust functions are defined. They use the fn keyword, followed by the function name, parameters with their types, and an optional return type.
fn: Declares a function.(param: Type): Defines parameters and their types.-> ReturnType: Specifies the function's return type.
Introducing `#[wasm_bindgen]`
To make a Rust function callable from JavaScript, we use a special attribute from the wasm-bindgen crate: #[wasm_bindgen]. Think of it as a bridge builder!
This attribute tells the wasm-bindgen tool to generate the necessary 'glue code' that allows JavaScript to understand and invoke your Rust function seamlessly.
Your First Exported Function: `add`
Let's write a simple Rust function that adds two integers and expose it to JavaScript. This function will live in your src/lib.rs file.
Notice the #[wasm_bindgen] attribute right above the function definition. This is the magic!
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Understanding `pub` and `use`
In the previous example, you saw pub fn and use wasm_bindgen::prelude::*;. Let's clarify their roles:
pub: This keyword makes the function public, meaning it can be accessed from outside the current module (e.g., by JavaScript).use wasm_bindgen::prelude::*;: This line imports essential items from thewasm-bindgenlibrary, including the#[wasm_bindgen]attribute itself.
Compiling Your Rust to WASM
After writing your Rust code, you'll use the wasm-pack build command (which we briefly mentioned in Lesson 1) to compile it into a WebAssembly module.
wasm-pack handles all the complexity, producing a .wasm file and a JavaScript 'glue' file that simplifies loading and interacting with your Rust functions from JS.
Another Simple Function: `multiply`
Let's add another function to our src/lib.rs. This one will multiply two numbers. The process is identical: define the function, add the #[wasm_bindgen] attribute, and make it pub.
This demonstrates how easy it is to expose multiple Rust functions.
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn multiply(a: i32, b: i32) -> i32 {
a * b
}Type Compatibility with JavaScript
When defining functions for WASM, wasm-bindgen automatically handles many common Rust types (like i32, f64) and maps them to their JavaScript equivalents (number).
For more complex types like strings or custom structs, special handling is required, which we'll explore in future lessons. For now, stick to primitive number types.
How JavaScript Accesses Exports
Once compiled, the wasm-pack tool generates a JavaScript file (e.g., my_wasm_lib.js) that acts as an intermediary.
You'll import this JS file into your web project, and it will provide functions like add() and multiply() directly, abstracting away the WASM loading process. This makes using Rust functions feel just like calling regular JavaScript functions.
Test Your Knowledge
What is the primary purpose of the #[wasm_bindgen] attribute in Rust when building for WebAssembly?
Recap: Functions for the Web
Great job! You've learned how to define and expose Rust functions for WebAssembly.
- Use
#[wasm_bindgen]to mark functions for export. - Ensure functions are
pub. wasm-pack buildcompiles your Rust into WASM and JS glue.- Primitive types like
i32map easily to JavaScript numbers.
Next, we'll dive deeper into how JavaScript loads and executes these WASM modules.
الأسئلة الشائعة
هل درس «كتابة دوال Rust لـWASM» مجاني؟
نعم — نص درس «كتابة دوال Rust لـWASM» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة WebAssembly (WASM) for High Performance Apps، انتقل إلى CoddyKit PRO. تتضمن دورة WebAssembly (WASM) for High Performance Apps 4 دروس في المجموع.
ماذا ستتعلم في «كتابة دوال Rust لـWASM»؟
طوّر دوال Rust يمكن ترجمتها إلى WASM وإتاحتها لبيئة JavaScript لتنفيذها تتمرن على WebAssembly (WASM) for High Performance Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ WebAssembly (WASM) for High Performance Apps؟
لا تُشترط خبرة سابقة. WebAssembly (WASM) for High Performance Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «كتابة دوال Rust لـWASM»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس WebAssembly (WASM) for High Performance Apps هذا؟
نعم. كل درس في WebAssembly (WASM) for High Performance Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد Rust لـWebAssembly
- كتابة دوال Rust لـWASM
- التكامل الفعّال بين JS و`wasm-bindgen`
- معالجة الأخطاء عبر حدود WASM