Note

I wrote the another article that uses wasm-bindgen instead of stdweb.

tl;dr

Steps

  1. Install cargo-web.
  2. Write your Rust library crate using the stdweb crate.
  3. Write a glue code with JavaScript.
  4. Deploy to Firebase.
  5. Happy!

Step 1: Install cargo-web.

$ cargo install -f cargo-web

Step 2: Write your Rust library crate using the stdweb crate.

For example, I create a library crate named greeter.

#[macro_use]
extern crate stdweb;

use stdweb::js_export;

#[js_export]
fn greet(name: &str) -> String {
    format!("Hello, {}", name)
}

Edit

You should add a twist to Cargo.toml file to build your crate.

[package]
name = "greeter"
version = "0.1.0"
authors = ["Sadaie Matsudaira <sadaie@matsuri-hi.me>"]

# ADD THIS TWO LINES
[lib]
crate-type = ["cdylib"]

[dependencies]
stdweb = "0.4.12"

End of Edit

Then, you should build your codes.

$ cargo web deploy --release

After finishing building, you can see three files under the directory, target/deploy/.

$ ls target/deploy/
greeter.js greeter.wasm index.html

Step 3: Write a glue code with JavaScript.

Copy the greeter.js and greeter.wasm files, then let's write Functions's code to deploy.

const functions = require('firebase-functions');
const greeter = require('./greeter');

exports.greet = functions.https.onRequest((request, response) => {
    response.send(greeter.greet("sadaie"));
})

Step 4: Deploy to Firebase.

Edit

Since Firebase Functions's default Node.js version is 6, WebAssembly is not available by default.
Therefore you should add engines setting to setting.json file.

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "engines": {"node": "8"},
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}

End of edit

$ firebase deploy --only functions

Step 5: Happy!

$ curl https://YOUR-FIREBASE-PROJECT.cloudfunctions.net/greet; echo
Hello, sadaie

That's fine😎