A talk about why using Rust for web development might be the right choice for you, and what to prepare for when starting.
Size: 11.77 MB
Language: en
Added: Jun 18, 2024
Slides: 33 pages
Slide Content
The Joy and Adventures of writing Rust for the web Rust Web Development
$ whoami Bastian Gruber Software Engineer for ~ 15 years Author of “Rust Web Development” (Manning) Professional Rust experience ~3 years Currently: Working on distributed Peer-to-Peer Systems in Rust rustwebdevelopment.com Mastodon: https://hachyderm.io/@bastian Twitter: @recvonline
$ date Todays talk: Rust Web Development Why Rust in web development How to get started What to bring with you Advantages Things to consider
The slide for your CTO - Why Rust for your microservice /API Thread-safe concurrency No memory-leaks* Saving $$$ on AWS Used by AWS, Microsoft, Google and others Excellent free learning resources Compiler enforces high standards and correctness
But why you actually want to write it in Rust The compiler is your pair-programmer who teaches you Excellent resources to learn and grow from Code standards are easily enforced through built-in tooling Documentation and testing is built-in High-standard third-party packages (crates) Compiles to a binary Freaking good type system :)
Rust 🧡 Web …or does it?
Rust 🧡 Web …or does it? No built-in HTTP
Rust 🧡 Web …or does it? No built-in HTTP No HTTP server
Rust 🧡 Web …or does it? No built-in HTTP No HTTP server No async runtime
Rust 🧡 Web …or does it?
Rust 🧡 Web …or does it? Rust wants to be a systems programming languages Least amount of overhead So it decided against: Adding a runtime to execute asynchronous code Implement HTTP Add a HTTP server to the library besides others…
Rust 🧡 Web …or does it? BUT , It brings the baseline to create TCP/UDP sockets write asynchronous code TCP/UDP + async/await + Future trait
What do you need for asynchronous programming ?
TCP behind the scenes use std::net::TcpListener; fn main() { let listener = TcpListener::bind("127.0.0.1:8080").unwrap(); for stream in listener.incoming() { let stream = stream.unwrap(); println!("stream accepted {:?}", stream); } ( longer version over here )
No runtime overhead Thread-safe async pub fn main() { let (tx, rx) = channel(); let x = Arc::new(RwLock::new(tx)); std::thread::spawn(move || { x.read().unwrap().send(4u8).unwrap(); }); dbg!(rx.recv().unwrap()); } error[E0277]: `Sender<u8>` cannot be shared between threads safely --> src/main.rs:8:5 | 8 | std::thread::spawn(move || { | ^^^^^^^^^^^^^^^^^^ `Sender<u8>` cannot be shared between threads safely | = help: the trait `Sync` is not implemented for `Sender<u8>` = note: required because of the requirements on the impl of `Sync` for `RwLock<Sender<u8>>` = note: required because of the requirements on the impl of `Send` for `Arc<RwLock<Sender<u8>>>` = note: required because it appears within the type `[closure@src/main.rs:8:24: 10:6]`
No runtime overhead Thread-safe async Rust compiler
No runtime overhead Thread-safe async Rust compiler Standardised tooling
And so much more… gRPC: tonic Peer-to-Peer: libp2p-rs Websockets: tokio-tungstenite In-Browser: WASM HTTP requests: reqwest Parsing JSON (besides others): serde
Example: Simple API with background task Live coding example…
Are there any downsides? Getting started with Rust takes longer Prototyping slower (strict compiler) You don’t want to go back to your previous language…
How to get started?
How to get started?
How to get started? Build something you like and use CLI Little helper web service Do the tokio tutorial Rust exercism Advanced reading
One more thing… Help creating types out of 3rd party APIs: Transform tools Use clippy to tidy up your code: cargo clippy -- -W clippy::pedantic Use cargo format rules to tidy up imports etc: Example .toml file Create documentation as part of your CI (GitHub example snippet): name: Build Documentation id: build_docs uses: actions-rs/cargo@v1 with: command: doc args: --all --no-deps