Rust Melbourne MeetUp - Rust Web Development

BastianGruber1 21 views 33 slides Jun 18, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

A talk about why using Rust for web development might be the right choice for you, and what to prepare for when starting.


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 )

That’s all hidden behind a framework, though

Example: axum #[tokio::main] async fn main() { let app = Router::new() .route("/", get(root)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn root() -> &'static str { "Hello, World!" }

Example: axum #[tokio::main] async fn main() { let app = Router::new() .route("/", get(root)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum::Server::bind(&addr) .serve(app.into_make_service()) . await .unwrap(); } async fn root() -> &'static str { "Hello, World!" } Async runtime: tokio

Example: axum #[tokio::main] async fn main() { let app = Router::new() .route("/", get(root)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum ::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn root() -> &'static str { "Hello, World!" } Async runtime: tokio async/await

Example: axum #[tokio::main] async fn main() { let app = Router::new() .route("/", get(root)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum ::Server::bind(&addr) .serve( app.into_make_service() ) .await .unwrap(); } async fn root() -> &'static str { "Hello, World!" } Async runtime: tokio async/await Web framework: axum

Example: axum #[tokio::main] async fn main() { let app = Router::new() .route("/", get(root)); let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); axum :: Server::bind(&addr) .serve( app.into_make_service() ) .await .unwrap(); } async fn root() -> &'static str { "Hello, World!" } Async runtime: tokio async/await Web framework: axum HTTP server: hyper

So, why then Rust and not NodeJS, Go?

No runtime overhead

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

Thank you! Question?
Tags