Skip to Main Content
Rust Programming Cookbook
book

Rust Programming Cookbook

by Claus Matzinger
October 2019
Intermediate to advanced content levelIntermediate to advanced
444 pages
10h 37m
English
Packt Publishing
Content preview from Rust Programming Cookbook

How to do it...

We are going to set up and run our own web server in just a few steps:

  1. Open src/main.rs first, and let's add some code. We will work our way down to the main function, starting with imports and a simple index handler:
#[macro_use]extern crate actix_web;use actix_web::{web, App, middleware, HttpServer, Responder, Result};use std::{env};use actix_files as fs;fn index() -> Result<fs::NamedFile> {    Ok(fs::NamedFile::open("static/index.html")?)}
  1. This is not going to be the only request handler, however, so let's add a few more to see how request-handling works:
fn path_parser(info: web::Path<(String, i32)>) -> impl Responder {    format!("You tried to reach '{}/{}'", info.0, info.1)}fn rust_cookbook() -> impl Responder { format!("Welcome ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Rust Web Programming

Rust Web Programming

Maxwell Flitton
The Complete Rust Programming Reference Guide

The Complete Rust Programming Reference Guide

Rahul Sharma, Vesa Kaihlavirta, Claus Matzinger

Publisher Resources

ISBN: 9781789530667Supplemental Content