diff --git a/content/wasm-languages/rust.md b/content/wasm-languages/rust.md index 34f8283f5..5552ef085 100644 --- a/content/wasm-languages/rust.md +++ b/content/wasm-languages/rust.md @@ -56,25 +56,18 @@ Things we're not big fans of: >> All of our examples follow [a documented pattern using common tools](/wasm-languages/about-examples). -Rust can use Spin's native executor as well as Spin's Wagi executor. We strongly recommend the native one, as it has more features. - When writing Spin applications in Rust, use `cargo init --lib` or `cargo new --lib`. Spin loads the `wasm` files as libraries, not as executables with a `main` function. -Here is an example `lib.rs` that uses Spin's native executor: +Here is an example `lib.rs`: ```rust -use anyhow::Result; -use spin_sdk::{ - http::{Request, Response}, - http_component, -}; +use spin_sdk::http::{IntoResponse, Request, Response}; +use spin_sdk::http_component; /// A simple Spin HTTP component. #[http_component] -fn hello_world(_req: Request) -> Result { - Ok(http::Response::builder() - .status(200) - .body(Some("Writing a very simple Spin component in Rust".into()))?) +fn hello_world(_req: Request) -> anyhow::Result { + Ok(Response::new(200, "Writing a very simple Spin component in Rust")) } ``` @@ -92,14 +85,8 @@ crate-type = [ "cdylib" ] [dependencies] # Useful crate to handle errors. anyhow = "1" -# Crate to simplify working with bytes. -bytes = "1" -# General-purpose crate with common HTTP types. -http = "0.2" # The Spin SDK. -spin-sdk = { git = "https://github.com/fermyon/spin" } -# Crate that generates Rust Wasm bindings from a WebAssembly interface. -wit-bindgen-rust = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "2f46ce4cc072107153da0cefe15bdc69aa5b84d0" } +spin-sdk = "3.0.1" ``` To build a Spin app in rust, use `cargo build`: @@ -113,38 +100,38 @@ $ cargo build --target wasm32-wasi --release The resulting binary can be run in Spin with a `spin.toml` that looks something like this: ```toml -spin_version = "1" -authors = ["Fermyon Engineering "] -description = "Hello world app." +spin_manifest_version = 2 + +[application] name = "spin-hello" -trigger = { type = "http", base = "/" } version = "1.0.0" +description = "Hello world app." +authors = ["Fermyon Engineering "] -[[component]] -id = "hello" -source = "target/wasm32-wasi/release/hello.wasm" -[component.trigger] +[[trigger.http]] +id = "trigger-hello" +component = "hello" route = "/" + +[component.hello] +source = "target/wasm32-wasi/release/hello.wasm" +[component.hello.build] +command = "cargo build --target wasm32-wasi --release" ``` -Note that we do _not_ add an `executor` line at the bottom of this file as we do in many other examples. +> Note: we've set the `hello` component's build command to be the `cargo build` invocation above, so that `spin build` will run it from now on. From there, running the app is as easy as `spin up`! -### Writing Wagi-Based Rust Apps - -It is also possible to write a Wagi application in Rust and run it in Spin or Wagi. Examples of this exist [in the Wagi examples repository](https://github.com/deislabs/wagi-examples). - ## Learn More Here are some great resources: -- The official [documentation for Spin](https://spin.fermyon.dev/rust-components/) has many examples, including creating Redis listeners. +- The official [documentation for Spin](https://developer.fermyon.com/spin/rust-components/) has many examples, including creating Redis listeners. - Rust has a [dedicated mini-site covering WebAssembly](https://www.rust-lang.org/what/wasm) - The Rust Linz group did a [presentation on Rust and Wagi](https://www.youtube.com/watch?v=9NDwHBjLlhQ) and posted a GitHub [repo full of Wagi examples](https://github.com/rstropek/rust-samples) - [Wasmtime](https://wasmtime.dev/) is the reference implementation of Wasm32-WASI. - [egui](https://www.egui.rs/) provides a GUI toolkit that can be compiled into Wasm and run in the browser -- DeisLabs has some [Rust Wagi examples](https://github.com/deislabs/wagi-examples) -- The [Bartholomew CMS](https://github.com/fermyon/bartholomew) is written in Rust and runs in Spin or Wagi +- There are several rich examples in the [Spin Rust SDK repo](https://github.com/fermyon/spin-rust-sdk/tree/stable/examples) as well as the [Spin Up Hub](https://developer.fermyon.com/hub) +- The [Bartholomew CMS](https://github.com/fermyon/bartholomew) is written in Rust and runs in Spin - The [spin-fileserver](https://github.com/fermyon/spin-fileserver) is a simple Rust Spin-native app -- There are several rich examples in the [Spin Kitchen Sink repo](https://github.com/fermyon/spin-kitchensink) \ No newline at end of file