Rust and WebAssembly Fundamentals Quiz Quiz

Explore essential concepts of using Rust with WebAssembly through this quiz, designed to reinforce core knowledge and best practices. Perfect for those looking to understand how Rust integrates with Wasm for web and non-web applications.

  1. Rust Compilation Target

    Which compilation target should you use to build Rust code for WebAssembly execution in browsers?

    1. x86_64-unknown-linux-gnu
    2. wasm32-unknown-unknown
    3. wasm64-unknown-linux
    4. asmjs-unknown-browser

    Explanation: The correct compilation target for Rust code to be used as WebAssembly in browsers is wasm32-unknown-unknown. This instructs the Rust compiler to target 32-bit WebAssembly. The other options are incorrect because wasm64-unknown-linux is not typically used for browsers, x86_64-unknown-linux-gnu is for native Linux, and asmjs-unknown-browser is not a valid Rust target.

  2. WebAssembly Output File

    When you compile a Rust project for WebAssembly, what is the typical extension of the output file containing the bytecode?

    1. .exe
    2. .dll
    3. .src
    4. .wasm

    Explanation: The compiled WebAssembly module from Rust will have the file extension .wasm, which stands for WebAssembly binary format. The .exe and .dll extensions are used for native executables and dynamic libraries, respectively, and .src is not a recognized output for this purpose.

  3. WebAssembly Binary Size

    Why is Rust often used with WebAssembly for performance-critical web applications?

    1. WebAssembly only supports interpreted languages
    2. WebAssembly ignores runtime errors
    3. Rust disables all type checking
    4. Rust produces small, efficient binaries

    Explanation: Rust is valued for its ability to create compact and high-performance WebAssembly binaries, making it suitable for demanding applications. The other options are incorrect: Rust enforces strict type checking, WebAssembly does not ignore errors, and it is designed for compiled—not interpreted—languages.

  4. Tool for Generating Bindings

    Which tool can you use to automatically generate JavaScript bindings for interacting with Rust-generated WebAssembly modules?

    1. js-sys
    2. wasm-bindgen
    3. bindgen-rs
    4. wasm-packager

    Explanation: wasm-bindgen is a tool that facilitates binding between Rust and JavaScript, making interoperation straightforward. bindgen-rs is for generating bindings to C libraries, not JavaScript, wasm-packager is not a standard tool for this task, and js-sys is a Rust crate providing JavaScript system types, not a binding generator.

  5. Exporting Rust Functions

    When exposing a Rust function to WebAssembly for access from JavaScript, which Rust attribute should be applied to the function?

    1. #[no_std]
    2. #[derive(Debug)]
    3. #[wasm_bindgen]
    4. #[main]

    Explanation: The #[wasm_bindgen] attribute tells the Rust compiler to expose the annotated function for JavaScript interoperation. #[derive(Debug)] is used for debugging traits, #[main] is not a valid attribute, and #[no_std] disables the standard library, which is unrelated to function export.

  6. WebAssembly Memory Access

    What must you manage carefully when passing large data buffers, like arrays, between JavaScript and Rust WebAssembly modules?

    1. Thread priorities in the browser
    2. Pointer alignment on the stack
    3. RAM overclocking settings
    4. The linear memory shared by both environments

    Explanation: Careful management of the shared linear memory is essential when transferring large data between JavaScript and Rust WebAssembly, as both access a contiguous block of memory. Thread priorities are not usually managed directly in the browser for Wasm, pointer alignment is handled by the compiler, and RAM overclocking is unrelated to this context.

  7. Calling JavaScript from Rust

    Which of the following best describes how you can call a JavaScript function from within Rust compiled to WebAssembly?

    1. Export Rust functions as public
    2. Use wasm-bindgen to import the JavaScript function
    3. Rename the function to 'main_js_call'
    4. Include the function in Cargo.toml dependencies

    Explanation: wasm-bindgen allows you to import JavaScript functions into Rust, making cross-language calls possible. Renaming functions or simply exporting Rust functions does not enable calling JS code, and Cargo.toml manages Rust dependencies rather than importing JS functions.

  8. Rust Data Types with WebAssembly

    Which Rust data types are most straightforward to share directly with JavaScript through WebAssembly?

    1. Traits and closures
    2. Integers and floats
    3. Hash maps and references
    4. Tuples and enums

    Explanation: Primitive types like integers and floats are easiest to exchange between Rust and JavaScript via WebAssembly due to their simple, well-defined representation. Tuples, enums, traits, and closures are more complex and require additional work for interoperability; hash maps and references are not shared directly.

  9. WebAssembly and DOM Access

    Can Rust-generated WebAssembly modules directly manipulate the Document Object Model (DOM) in a browser environment?

    1. Yes, accessing DOM APIs directly from Wasm
    2. Yes, but only for SVG elements
    3. No, WebAssembly cannot communicate with JavaScript at all
    4. No, but they can call JavaScript functions that do

    Explanation: WebAssembly does not have direct access to the DOM; instead, Rust-generated Wasm must call out to JavaScript functions for any DOM manipulation. Direct DOM access is not available; it is also not limited to SVG. The statement that Wasm cannot communicate with JavaScript at all is incorrect.

  10. Primary Benefit of Using Rust with WebAssembly

    What is a primary reason to choose Rust for WebAssembly development over a purely JavaScript-based approach?

    1. Rust runs natively on all browsers without compilation
    2. Rust automatically updates all HTML content
    3. JavaScript cannot handle logical operations
    4. Rust enables better performance for compute-intensive tasks

    Explanation: Rust can be compiled to WebAssembly to achieve higher performance in compute-heavy scenarios compared to traditional JavaScript implementations. Rust does not update HTML content automatically, JavaScript is fully capable of logical operations, and Rust code must be compiled before running in browsers.