The Detroit Post
Thursday, 21 October, 2021

Rust For The Web

author
Maria Garcia
• Saturday, 21 November, 2020
• 9 min read

You can find the second article in this series (“deploy your first Rust app”) over here. You can pick up Python or Ruby over the weekend, create a first CRUD application and be happy with the results.

rust pc access early herunterladen gameplay kostenlos general rusty versione completa gratis indirme linkleri inceleme ve yeah said oh title
(Source: yourmomrocks.wordpress.com)

Contents

You then will order the Rust book, see its size, sigh and get started. After a few weeks of fighting through the book after work, you give up and wait until someone else creates an easy-to-follow tutorial.

Life circumstances however gave me a few months time on my hands to really focus on Rust. In the coming weeks and months, I’ll publish a series of articles to help you to get from concept to product.

After installing them (I chose brew for macOS in this example, the method doesn’t matter), the underlying stack looks different. Node covers the whole stack, and offers with Goa and Express, two well-known and “rock-solid” web frameworks which help you to build applications on top of HTTP.

The current web frameworks (act ix and rocket) are implementing everything up until HTTP though. If you want to use pure HTTP calls without any larger framework, you can install “crates” (equivalent to NPM packages in the Node world) which implement the HTTP protocol (like hyper and tiny_http).

Unlike NPM i which fetches the packages right away, and will add it to the package.Jason with the save notation. The website arewewebyet.org is tracking the progress and showing you interesting packages in the Rust world.

(Source: www.flickr.com)

There is also an attempt to create an official Rust Web Framework, called Tide. Feel free to contribute and help craft a great environment for web development in Rust.

Although not always easy to understand and handle, Promises and the event loop are what makes Node so lucrative. Rust is also implementing an asynchronous mechanism, which are not yet in the final version.

A library called Tokyo is already offering an asynchronous run time. You can track the progress on asynchronous programming in Rust over at areweasyncyet.

To not get frustrated until my next post, here are the main four things about Rust you will have to get used to (which are quite awesome after a while). Rust has a type called Option, which encapsulates either the return value or None.

In short: Every assignment (=) to a non-trivial type (everything which doesn’t have a fixed size) moves the ownership over. In this example, our HTTP crate request is returning a Response struct (type) which implements certain methods.

rust solo
(Source: www.youtube.com)

In the next few days, weeks and months I will cover the basics up until creating a solid web application. Rust has a similar syntax to C++ but can guarantee memory safety by using a borrow checker to validate references.

Since 2016 Rust is voted every year, in the Stack Overflow developer survey, as the “most loved programming language”¹. Rust, has quickly established a fan base at Microsoft, Amazon Web Services (AWS), and other tech companies.

In their webpage, Rust says that it comes with the command line, web assembly, networking, and embed built-in. In this article, I will stick with the networking part as we will try to build an example of a simple API with a “Hello World” JSON response.

To install Rust just run the following command in your shell. To build a web server and after some research, I choose to use the act ix framework.

Act ix is a powerful, pragmatic, and extremely fast web framework for Rust. Got to your terminal in the project directory and hit: cargo run This command will download all the dependencies and compile the code into a binary file.

texture grunge contrast textures resolution background dirt photoshop metal concrete textured wall paper silver lost taken textur architecture backgrounds grey
(Source: jooinn.com)

Getting started is easy because of the documentation and the fact that is a compiled language and is memory safe can make it a great tool to develop all sorts of software in the future. Working with Rust is fun and will do it more often in the future in my projects, and I am excited to learn even more.

My primary goal when I started learning Rust is to make a move from my ducky JavaScript tech stack (Node.js Express), that means, to make web apps in Rust. There are many approach to creating web apps in Rust, for example, compile Rust code to client-side JavaScript, writing a RESTful API, or building an isomorphic web app just like it's 2012.

This is mean, you need to find a way to run Rust code on the web browser. Thanks to ASM.js and Web Assembly, this is possible, with a quick setup, you can compile your Rust code to ASM.js/WebAssembly in few minutes.

Now, for the component-based architecture, I just created some macros to wrap over the std web crate and rendering HTML elements via browser's DOM API. Who need to React anymore when you can even write an click event in your Rust code :D (just kidding).

For simplicity, you can use nickel.rs, it's one of the most lightweight frameworks in Rust that inspired by Node's Express. Personally, I prefer to use Rocket as it is a very good framework that has many features, but still keep your code simple, with very elegant syntax.

brick wall rusted bricks texture masonry background rust jooinn
(Source: jooinn.com)

The only minus point for this framework is it required to use nightly Rust version. So I can put all my frontend code inside WWW folder, and access it along my RESTful API.

The last one, my favorite one, like it's 2012, when I started my programming career with my first PHP job. Rocket and many other frameworks has the ability to render the HTML templates after binding some data into it.

I hope the three approaches I listed here will be helpful if you're the one who asking: “Can Rust make webs?” Rust is gradually becoming the language of choice for many developers who want to build efficient and reliable applications more quickly while still maintaining flexibility and low-level control.

If the above command results in an error, click here to see rust up installation instructions. With rust up installed, we can use Cargo to create a new Rust project.

The --bin flag tells Cargo to generate this as a binary-based project. Rocket uses unstable features of Rust, like its syntax extensions.

rust
(Source: www.youtube.com)

In the above block of code, we started by using the attribute, #get(“/hello”)] to tell Rocket that our function expects a GET request to the /hello route. Next, we named our function hello() and specified its return type as Jason with a <&'static STR> argument.

This means that when a GET request is sent to our /hello route, it will return a JSON response with body of 'status': 'success' and 'message': 'Hello API!' Finally, we used the launch() method to start the application server and listen for requests.

We can now run cargo build on our terminal to compile our Rocket application. Finally, we can test our application and route by navigating to http://localhost:8000/api/hello on our browser or API client.

We’ve successfully launched our first Rocket API, but this is just the beginning. The Rocket documentation does a great job in explaining the different return and response types.

We’ll use our POST route to add book information to a dummy database. Rocket has support for databases like MySQL, Postgres, SQLite, Redis, and MongoDB.

rust tips
(Source: www.youtube.com)

Firstly, let’s define what type of information we’ll be expecting from our user when they send a request to our book route. This time around, we added the type of data Rocket should expect when watching for requests as the second argument of the route attribute #.

Next, we’ll add the # attribute to our Book struct. In the above block, we used the book_form.into_inner() method to get the request body from our user, then we defined our dummy database as a vector with type Book and pushed the data we received from our user to it using the dummy_db.push(book) expression.

We can use cargo-watch to compile and run our application so that we don’t have to rebuild every time we make changes to our app. In the above block, we started by using the # attribute to tell Rocket to return a 404 error when this route is called.

To test our not_found route, let’s navigate to a path that does not exist from our browser or API client. If you notice, inside our tag and in the left-side div, we have a paragraph with class of sub-title that contains Hello {{first_name}} {{last_name}} .

Now that we’ve added the values we used in the home.HBS file, let’s return our template with the data we just created: Since this is our landing page, we’re using a different base path “/” so that we only need to navigate to localhost:8000 to see our rendered template.

metal rust dirty painted background royalty
(Source: www.dreamstime.com)

In this article, we’ve introduced Rust for the web through the Rocket framework. We covered the basics of Rocket, how to set up web APIs, response types, error handling, and rendering HTML through the Handlebars' template engine.

While Rocket is a good fit for building web APIs, it might not be the best choice for handling frontend rendering, like we did in the last part of this article. However, Rust shines in this area through the Yew framework, which was built for creating multi-threaded frontend web apps with Web Assembly.

If you’re interested in monitoring and tracking performance of your Rust apps, automatically surfacing errors, and tracking slow network requests and load time, try Rocket. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred.

Other Articles You Might Be Interested In

01: Mr Real Estate Corpus Christi
02: Rpm Real Estate Little Rock Ar
03: Rps Real Estate Fresno Ca
04: Aqua Real Estate Scottsdale Tasmania
05: Videos Do Avakin Life
06: View City Of Detroit Property Taxes Online
07: Village Real Estate Co Indianapolis
08: Village Real Estate Indianapolis
09: Vinegar For Rust Removal
10: Virginia Beach Real Estate Gis
Sources
1 www.vbgov.com - https://www.vbgov.com/government/departments/communications-info-tech/maps/Pages/default.aspx
2 gisapps.vbgov.com - https://gisapps.vbgov.com/map/
3 www.vbgov.com - https://www.vbgov.com/property-search
4 buckingham.interactivegis.com - http://buckingham.interactivegis.com/
5 www.co.northampton.va.us - https://www.co.northampton.va.us/government/open_government/maps
6 wisecounty.org - http://wisecounty.org/GIS/gis_strategicplan.html
7 www.glassdoor.com - https://www.glassdoor.com/Job/chesapeake-geography-jobs-SRCH_IL.0,10_IC1130279_KO11,20.htm
8 www2.erie.gov - https://www2.erie.gov/gis/index.php
9 colonialbeachva.net - https://colonialbeachva.net/
10 statelaws.findlaw.com - https://statelaws.findlaw.com/virginia-law/property-line-and-fence-laws-in-virginia.html
11 www.harrisonburgva.gov - https://www.harrisonburgva.gov/personal-property-student-vehicles
12 search.yahoo.com - https://search.yahoo.com/tablet/s