Hone logo
Hone
Problems

Simple Rust WASM Module: Add Two Numbers

This challenge guides you through creating a basic Rust program that compiles to WebAssembly (WASM). WASM allows you to run code written in languages like Rust in web browsers and other environments, offering performance benefits and portability. Your task is to write a Rust function that adds two integers and then build it as a WASM module.

Problem Description

You need to create a Rust project that defines a function add which takes two 32-bit integers as input and returns their sum as a 32-bit integer. This function should be exposed as a WASM function, meaning it can be called from JavaScript (or other WASM-compatible environments). The project should be structured to compile specifically for the WASM target.

Key Requirements:

  • Function Definition: Implement the add function in Rust.
  • WASM Target: Configure the Rust project to compile for the WASM target.
  • Export: Ensure the add function is exported from the WASM module, making it accessible from JavaScript.
  • No Main Function: The WASM module should not have a main function. It's intended to be a library that's loaded and used by another program (like JavaScript).

Expected Behavior:

When the WASM module is loaded and the add function is called with arguments a and b, it should return the integer a + b.

Edge Cases to Consider:

  • Integer Overflow: While the problem doesn't explicitly require handling overflow, be aware that Rust's default behavior for integer overflow is to panic in debug mode and wrap around in release mode. For this simple example, we'll assume overflow is not a primary concern.
  • Input Validation: The problem doesn't require input validation. Assume the inputs are valid 32-bit integers.

Examples

Example 1:

Input: a = 5, b = 10
Output: 15
Explanation: The function `add(5, 10)` should return 15.

Example 2:

Input: a = -3, b = 7
Output: 4
Explanation: The function `add(-3, 7)` should return 4.

Example 3: (Edge Case - Large Numbers)

Input: a = 2147483647, b = 1
Output: -2147483648 (due to integer overflow wrapping)
Explanation:  Demonstrates the potential for integer overflow.  The result is the minimum 32-bit integer.

Constraints

  • Target: The code must compile for the wasm32-unknown-unknown target.
  • Function Signature: The add function must have the signature fn add(a: i32, b: i32) -> i32.
  • No main function: The Rust source file should not contain a main function.
  • Dependencies: You are allowed to use the wasm-bindgen crate if you find it helpful for exporting the function, but it is not strictly required for this basic challenge.

Notes

  • You'll need to use Cargo to create and manage your Rust project.
  • The wasm32-unknown-unknown target is a generic WASM target.
  • Consider using #[wasm_bindgen] attribute if you want to easily export the function to JavaScript. However, a simpler approach without wasm-bindgen is also acceptable.
  • Focus on getting the core add function and WASM compilation working first. JavaScript integration is beyond the scope of this challenge.
  • The goal is to demonstrate the ability to create a WASM module from Rust code.
Loading editor...
rust