Extremely Simple Rust Rocket Framework Tutorial | June 2020

rust rocket

Rust Rock Framework

Ok, if you are stuck, scratching your head, and regretting why you even considered delving into rust rocket web framework. Considering that rust language itself is really not that easy to get comfortable in itself. Gathering strength to get into a rust web framework is also not a minor feat. If you are feeling dizzy and do not have proper guidance regarding what to do, then I welcome you to the rocket framework tutorial.

We will be very similar to the documentation in this rust rocket framework tutorial. The documentation is good. When I was reading it, I felt right at home but the documentation seems to go all over the place once you start to get too deep. All of the concepts are defined but they are not connected in a way that someone new can make sense of it. Keep in mind that there is a lot of material in that documentation. Considering that the title of this article is “Extremely simple rust rocket framework tutorial”, explaining every single little detail might be very overwhelming.

In order to get you going, I will teach you in the old fashioned way. We will get our hands dirty with the code. Do not panic though, this rust rocket framework tutorial is extremely simple, and if you have an intermediate understanding of the rust language than you will be just fine. The whole point of this rust rocket framework tutorial is that we will make a simple rust rocket app and during this process, I will explain every step that we will take. Let us jump into it.

What Are We Doing?

Let me explain the main objective of this rocket app. We will start by making a very simple home page. The user will enter a path and then a number. We will be taken to a new page which will display a new number that is attained after adding 100 to the previous number. It will look something like this: “http://localhost:8000/add/50”. After typing this in the browser, the user will be taken to a page that will display “150”. This process will help you in understanding, how you can make an HTTP GET request in this rust web framework. You will also learn, how you can return a response.

Everything is clear? well, hopefully, the object is clear. Let us properly begin this rocket framework tutorial.

Initialization

Before we can do anything, we will have to change to the rust language nightly mode. The Rust Rocket framework depends on the rust’s nightly version. In order to change to the nightly version, all you have to do is to type this in the terminal.

rocket framework tutorial, rocket app, rust web framework, rust language

This command will install the rust’s nightly version on your computer and it will also change your current toolchain to the nightly version. If you type “rustc –version” in the terminal, you will see that along with the version number it also says “nightly”.

Ok, so rust language is all set up, now we will have to make a rust binary in order to work in this rocket web framework. type this command in the terminal.

rocket framework tutorial, rocket app, rust web framework, rust language

The cargo new command will make a new rust binary for you to work in. The usual cd command will take you in that directory. Now you can open your favorite editor and let us get going in this rust rocket framework tutorial. It took a lot of setting up but now I am serious, we are finally ready to work.

Open up the Cargo.toml file and add the rocket framework as a dependency. The cargo will handle everything after that.

rocket framework tutorial, rocket app, rust web framework, rust language

After this, we have to work in the main.rs file. We do not need any other file right now, we will do everything in the rocket app file.

Adding the Attributes

We will first add some boilerplate attributes that do not make any sense at first

rocket framework tutorial, rocket app, rust web framework, rust language

The first line enables the unstable features. In this rocket framework tutorial, we are using the nightly version, typing this line is important or you will get errors later. If you want to read more, you can check this out. This article has no documentation during the writing of this article but it will be written later.

The second line is the rust language way of importing any module, crate, or function into the working file.

The last 2 lines import an Html type, we will need this in order to return an Html response. The Form is a struct that we will need to work with Html forms. This is how we roll in rust language.

Making the Main Function

The main function is the simplest yet the most important piece of code that we need in this rust rocket framework tutorial. The main function will start the server along with mounting all the request handlers.

rocket framework tutorial, rocket app, rust web framework, rust language

 

As you can see, the main function will act as the engine in this rocket app. In the whole of this code, the most interesting part is the mount("/", routes![index, add]) method. The first argument that is being passed in this method is for creating a namespace. Imagine that you have a very big website. You will have so many routes and sections. If there is route like this: “users/<some_user_id>”. This route can be namespaced and anything after the namespace will be considered as the part of that namespace. In our example, we have namespaced the default root path.

The second argument takes the name of the routes handlers functions that are associated with that namespace, If you create a new handler, you will have to add the name in the arguments of the mount method. We will later define these handlers in this rocket framework tutorial.

The Index Route

Now we will create the root route or more generally known as the home page of our rust rocket app. Here is the code.

rocket framework tutorial, rocket app, rust web framework, rust language

If you have worked in Python then the first line will be eerily similar. In order to tell the rocket framework that the function we will define below is a handler, we will have to write this attribute. All in all, this line simply tells the rust web framework that: Hey!!! if the user types “/” after the domain name, you will have to run the function that is defined down below. Note, that the request has to be HTTP GET request if it is something else than the default 404 error page that is defined in the rocket will be returned.

This function is simply returning a String. You can return any type from these handlers. In the rocket, almost every type implements a Responder trait. You can also implement this trait on the custom types you define. The rocket will take the return value, wrap it up nicely in an HTTP Response along with all the necessary metadata, and return it to the client.

If you run this app using cargo run, all the necessary dependencies will be installed first, the executable will be built and then the rocket will say that the server has started at localhost:8000. You will see something similar to this.

rocket framework tutorial, rocket app, rust web framework, rust language

Congratulations!! you have created your first rocket app but there is more to this rust web framework. This rocket framework tutorial does not end here, we still have things to do. So, let’s go.

Returning the Html

Just displaying simple “Hello World” is not that exciting. In order to create a nicer environment, we will have to return an Html document. In order to do that we will have to make some minor changes in our index function code.

rocket framework tutorial, rocket app, rust web framework, rust language

The function is now returning an Html<&'static str>. The &'static str also implements the responder trait. You can say that the Responder trait is wrapping over this return type.

In the body of the function, we will have to write a function and pass in the raw strings. We wrote r#””# because Html syntax is full of character that might get escaped during the compilation process so, in order to tell the compiler that what we are writing is exactly what we want it to be, everything will be passed as it is and nothing will be escaped.

Now if you run the rocket app and go to the root path, you will see that on the tab of the browser, “Home” is written. The main body has a welcome text and instructions on how to use this app.

Creating the Add Functionality

We will need another route to enable users to input a number and get another number back. In order to understand, I will display the code here

rocket framework tutorial, rocket app, rust web framework, rust language

As you can see that the request that the user makes has to be HTTP GET. Users will have to enter something like this: “/add/20”. The <number> part is called the dynamic parameter. If you see in the function signature, the number will be parsed into i32. The user can enter anything in the URL but if it cannot be parsed into an i32, the rocket will ignore that handler and move on, if it cannot find any function that can parse the request than the 404 Html page will be returned.

The rest of the code is pretty simple to understand. The String type implements the Responder trait, so it can be returned from the function. If you run this application and go to the address bar of the browser and type “http://localhost:8000/add/50”, you will be taken to another page that will say “You entered 20, We added 100, the new number is 120”.

Rust Books

Hands-On Microservices with Rust

This book gives a comprehensive guide in developing and deploying high-performance microservices with Rust. And it has a really good number of positive reviews.

 Check Price On Amazon

Hands-On Data Structures and Algorithms with Rust

This is also a great book that explains about everything you need to learn about data structures and algorithms with rust.

Check Price On Amazon 

Conclusion

You have learned a lot in this rust rocket framework tutorial. You now know how to handle the user’s GET requests and show the appropriate response. I hope this simple rocket app has cleared your confusion.

Rust language is hard but this rust web framework was very easy to work with. In order to feel comfortable with the syntax, you will have to type the code that is provided in this rocket framework tutorial. Read this article again and again if you are facing any problem. Now, when you have a basic understanding, you can now look at the documentation with much more confidence.

Exercise

If you want to gain command in rust rocket and in order to gain experience in rust rocket, you will have to do exercises that will turn your gears. I will list out some exercises that you can do.

Make a rocket app that takes two dynamic parameters. One has to be a boolean value and one has to be a &’static str. If the boolean is true then the name is displayed, if the value is false then the name should not be displayed.

If you liked this article about rust rocket framework you can also check out

4 thoughts on “Extremely Simple Rust Rocket Framework Tutorial | June 2020”

  1. I seriously love your site.. Great colors & theme. Did you build this website yourself?
    Please reply back as I’m trying to create my own blog and would love to find out where you got this from or just what the theme is called.
    Kudos!

Leave a Reply

Your email address will not be published. Required fields are marked *