Server Basics

Learn how to spin up a server in Rust and respond to incoming connections.

Having the data on your machine and elaborating on it is just one part of the modern data endeavor. Data should, in fact, be available remotely through servers and web services.

The basics of web applications in Rust

Rust has many different web frameworks that can be used to spin up a server. There’s not a single preferred choice, nor is there a full-fledged solution. We can’t replace Laravel, Rails, or Django with a Rust equivalent, but there are lighter frameworks equivalent to Flask or Sinatra.

For this course, we’ll explore Warp, which is like a more robust version of Express.js.

First, let’s prepare our Cargo.toml, like so:

[dependencies]
warp = "0.3.2"

Then, let’s spin up a server:

Press + to interact
use warp::Filter;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
server_setup!(3); // Needed for this playground to work
let routes = warp::any().map(|| "Hello, World!");
println!("Server started on: http://127.0.0.1:3030");
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}

Note: As usual, you can ignore the ...