22,79 €
Looking for a fast, powerful, and intuitive framework to build web applications? This Rust book will help you kickstart your web development journey and take your Rust programming skills to the next level as you uncover the power of Rocket - a fast, flexible, and fun framework powered by Rust.
Rust Web Development with Rocket wastes no time in getting you up to speed with what Rust is and how to use it. You’ll discover what makes it so productive and reliable, eventually mastering all of the concepts you need to play with the Rocket framework while developing a wide set of web development skills. Throughout this book, you'll be able to walk through a hands-on project, covering everything that goes into making advanced web applications, and get to grips with the ins and outs of Rocket development, including error handling, Rust vectors, and wrappers. You'll also learn how to use synchronous and asynchronous programming to improve application performance and make processing user content easy.
By the end of the book, you'll have answers to all your questions about creating a web application using the Rust language and the Rocket web framework.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 384
Veröffentlichungsjahr: 2022
A practical guide to starting your journey in Rust web development using the Rocket framework
Karuna Murti
BIRMINGHAM—MUMBAI
Copyright © 2022 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Associate Group Product Manager: Pavan Ramchandani
Publishing Product Manager: Aaron Tanna
Senior Editor: Mark Dsouza
Content Development Editor: Divya Vijayan
Technical Editor: Shubham Sharma
Copy Editor: Safis Editing
Project Coordinator: Rashika Ba
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Production Designer: Nilesh Mohite
Marketing Coordinators: Anamika Singh and Marylou De Mello
First published: June 2022
Production reference: 1240622
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-80056-130-4
www.packt.com
To my family, Ing, Ping, and Ling, for their 7 years of patience. To my mom, Tjong Njoek Fa, who always supported me. To my dad, Lestoro, who introduced me to the wonderful world of books, reading, and writing.
– Karuna Murti
Karuna Murti started out as a network engineer in Indonesia back in 2005. After that, he continued his career as a web developer, engineering manager, and chief engineer at a consulting company, using various languages such as PHP, Ruby, JavaScript, Fortran, and Python. He also worked with web applications and frameworks such as Magento, WordPress, and Ruby on Rails. In 2015, after establishing his own consulting company, he moved to Japan and now works as a technical lead at Merpay, one of the leading mobile payment apps in Japan. He is helping the company to build a cashless society by leading a team that connects millions of users with thousands of financial institutions in Japan. Even though he uses Go as the main language in his day job, Rust and Rocket have been a passion of his ever since their release.
I want to thank Tomohiro Kato for checking the outline of this book. Many thanks to Robert Jerovšek for reviewing this book.
Robert Jerovšek is an experienced software engineer who enjoys challenges and has built various types of applications, ranging from mobile to web, but with a particular focus on developing robust backend systems for fast-growing services. He also has experience in management and is currently working as an engineering manager in Japan. Previously, he worked as a unit lead in South Korea and was the CTO of an EdTech start-up in Spain.
Rocket is one of the first web frameworks in the Rust programming language. Rocket provides complete tooling to build web applications, such as tools for routing the requests and the strong typing of the incoming requests, and middleware to manipulate incoming requests and outgoing responses. Rocket also provides support for templating and connections to various databases.
Rocket is a web framework written in the Rust programming language. As one of the newer programming languages, Rust is designed to be performant and safe. It is easy to create a safe, multithreaded, and asynchronous application. Rust also has a strong foundation in its tooling, documentation, and community packages. All those strengths contribute to Rust's rapid growth in popularity.
This book explores building a complete web application using the Rocket web framework and the Rust programming language. You will be introduced to various techniques to build a web application that can handle incoming requests, store the data in an RDBMS, and generate proper responses back to any HTTP clients.
We wrote this book to help software engineers who want to learn how to use the Rocket web framework to build web applications. Although not mandatory, basic knowledge of the Rust programming language will help you understand the topics covered easily.
Chapter 1, Introducing the Rust Language, introduces the Rust language and the tools to build Rust applications.
Chapter 2, Building Our First Rocket Web Application, guides you through creating and configuring a Rocket application.
Chapter 3, Rocket Requests and Responses, introduces the Rocket routes, requests, and responses.
Chapter 4, Building, Igniting, and Launching Rocket, explains two important components of Rocket: state and fairings. State provides reusable objects, and fairings act as the middleware part of the Rocket application. This chapter also explains how to connect a database to the Rocket application.
Chapter 5, Designing a User-Generated Application, explores the process of designing an application and shows how to use Rust modules to create a more manageable web application.
Chapter 6, Implementing User CRUD, guides you on how to create, read, update, and delete (CRUD) objects in the Rocket web application and the database behind the Rocket application.
Chapter 7, Handling Errors in Rust and Rocket, explains how to handle errors in Rust and how we can apply error handling in the Rocket application.
Chapter 8, Serving Static Assets and Templates, shows how to serve files (such as CSS files and JS files) using the Rocket web application. You will also learn how to use a template to create a response for the Rocket web application.
Chapter 9, Displaying Users' Post, guides you through Rust generics and how to use generics to display different types of user posts.
Chapter 10, Uploading and Processing Posts, explains asynchronous programming and multithreading in Rust applications and how to apply these to process user uploads in the Rocket web application.
Chapter 11, Securing and Adding an API and JSON, guides you through creating authentication and authorization in a Rocket web application. This chapter also explains how to create JSON API endpoints and how to secure API endpoints with JWT.
Chapter 12, Testing Your Application, introduces you to testing the Rust application and creating an end-to-end test for the Rocket web application.
Chapter 13, Launching a Rocket Application, explains how to configure the production server to serve requests using the Rocket web application. This chapter also explains how to containerize the Rust application using Docker.
Chapter 14, Building a Full Stack Application, explains how to use the Rust programming language to build a frontend WebAssembly application to complement the Rocket web application.
Chapter 15, Improving the Rocket Application, explains how to improve and scale the Rocket web application. This chapter also introduces you to possible alternatives to the Rocket web framework.
You will need a Rust compiler by installing Rustup and the stable toolchain on your computer. You can use Linux, macOS, or Windows. For macOS users, it is recommended to use Homebrew. For Windows users, it is recommended to install Rustup using the Windows Subsystem for Linux (WSL or WSL 2).
All the code in this book has been tested on Arch Linux and macOS, but they should work on other Linux distributions or the Windows operating system as well.
Since Rust is a compiled language, various development headers might have to be installed on your computer, for example, libssl-dev or libpq-dev. Pay attention to error messages when compiling the code sample in this book, and install the required libraries for your operating system and development environment if needed.
Further along in the book, in Chapter 10, we are going to process videos using the FFmpeg command line.
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book's GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/Rust-Web-Development-with-Rocket. If there's an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots and diagrams used in this book. You can download it here: https://packt.link/PUFPv.
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Built-in data types, such as Option or Result, handle null-like behavior in a safe manner."
A block of code is set as follows:
fn main() { println!("Hello World!");}When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
impl super::Encryptable for Rot13 { fn encrypt(&self) -> String { }}Any command-line input or output is written as follows:
rustup default stable
rustup component add clippy
Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: "Select System info from the Administration panel."
Tips or Important Notes
Appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected] and mention the book title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Once you've read Rust Web Development with Rocket, we'd love to hear your thoughts! Please click here to go straight to the Amazon review page for this book and share your feedback.
Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.
In this part, you will learn about the Rust programming language, including the basics of it, how to install it on your operating system, and how to use the Rust tools and package registry. You will also create a Rocket web application and configure, compile, and run it. We will look at installing and including other packages for web applications. We will use sqlx for web application connection to relational databases.
This part comprises the following chapters:
Chapter 1, Introducing the Rust LanguageChapter 2, Building Our First Rocket Web ApplicationChapter 3, Rocket Requests and ResponsesChapter 4, Building, Igniting, and Launching RocketChapter 5, Designing a User-Generated Application