How to Build App with Rust

Dec 23, 2022

How to Build App with Rust

Rust is the latest programming language on the rise. Developed at Mozilla over the last six years, Rust is an open-source project that offers a new approach to memory management and memory safety.

How to Build App with Rust
Source: LogRocket blog

It is used in expanding commercial software projects and engineered systems. The language features explicit control over how slices of data are managed, which makes it highly attractive for complex tree structures with deep nesting.

Why is Rust so popular right now?

There are so many reasons why Rust is the champion right now:

  1. The safety. Rust provides a great deal of safety in your program by helping you write safer code and help you avoid common mistakes.
  2. Reliability. Rust helps you write more reliable code by avoiding common mistakes when dealing with error handling and exception handling.
  3. Fast execution speed of Rust programs compared to other programming languages in recent times, with an increasing number of high-profile projects using Rust for their high-performance needs.
  4. Great Community across the globe works together to improve the implementation, libraries, and tools for Rust programmers using a modern language development technique called "Concurrency."
  5. Cross-Platform Friendly, Rust supports all operating systems (Linux, Mac OSX, and Windows) and hardware architectures (x86, x86-64, ARM32), including a growing number of embedded systems.
  6. Rust enables the creation of fast, robust servers, desktop GUI applications, and high-performance networking programs.

Rust web frameworks

  1. Iron: Iron is a web framework for Rust. It provides an HTTP server, middleware, and request handlers. It works excellently with frameworks such as [Webdy] or [Iron-Cloak].
  2. Diesel: Diesel is a web framework for Rust, created to make the development of large-scale applications easy and productive. Flask and Chart inspired it, but it has its flavor of the response lifecycle.
  3. Chai ("Cheese"): Chai is a small library providing helpers to unit test JavaScript code in Rust programs with libsodium as the cryptography backend via libsodiumoxide. Chai is a port of Chai.js, with some changes and fixes.
  4. Rocket: Rocket is a high-performance web framework for Rust. It has integrated automatic HTTPS, a highly optimized HTTP 1.1 and HTTP/2 implementation based on Rust's task-driven asynchronous IO tools, and cross-platform support for both Unix and Windows.
  5. Iron web framework: Iron web is a lightweight web framework for building rapid API's in Rust. This allows you to spin up services like databases and clients to APIs quickly.

How to make web app with Rust

While many people may ask, "what is rust programming used for?", below is a step by step build up that you can try during your free time.

Rust is a systems programming language that emphasizes safety, speed, and concurrency. In this post we will build an app with cargo, Rust's package manager and build tool. Cargo allows you to compile Rust code and manage dependencies. In this post we will build a simple HTTP server using cargo's command line tool.

Step 1

Make a directory for your project.

mkdir hello-rust-server
cd hello-rust-server

Step 2

Start a cargo project by executing the init command, which creates a new cargo project with a Cargo.toml file.

mkdir -p src/hello-rust-server
cargo init --bin demo_app_server

You should see the following output:

Create a new crate based on the project template? [Yn] Y
Create name [demo_app_server]? HelloRustServer 
Enter new project name: demo_app_server 
Created. /src/hello-rust-server (for library) src/main.rs (entry point) src/lib.rs (components) Cargo.toml readme.md license Created ./Cargo.toml

Step 3

Compile our rust code and output a binary executable.

mkdir -p build/demo_app_server/debug/
cd build\demo_app_server\debug\
cargo build --release --lib ./src/hello-rust-server .. info:::warning: the `.cargo` directory is empty! 
Compiling hello-rust-server v0.1.0 (/Users/Jessica/dev/blog) Finished release [optimized] target(s) in 39.12 secs 
Running `target\debug\hello-rust-server` info:::path: `/Users/Jessica/dev/blog/build\demo_app_server\debug\target\release\hello-rust-server` 
running `target\debug

Step 4

After compiling, you should see the hello-rust-server executable on your path. If you double click on it, you will see our example server running.

info:::path: `/Users/Jessica/.cargo/bin` ✓ #HelloRustServer v0.1.0 
/Users/Jessica/dev/blog/hello-rust-server (built)

Copy the built executable to your project.

cp target\debug\hello-rust-server src/main.rs src/main.rs:7:1: error: no method named `run` in the type `std::process` src/main.rs:7 println!("Hello Rust!"); ^~ src/main.rs:8:1: error: no method named `run` in the type `std::env::args` src/main.rs:8 println!("Hello World"); ^~ src/main.rs 1 error generated. Run and test this project via cargo test .
Running `target\debug\hello-rust-server`

If you see the following output, then you've been successful!

INFO: Loading config file std.toml INFO: 0 tasks reported as running INFO: Running `target\debug\hello-rust-server` Hello Rust! Hello World!

Copy your project to a different directory and make sure it still works. You should see the same output.

cp -R target build/demo_app_server/release/
cd build\demo_app_server\release\
cargo run --lib./src/hello-rust-server .. info:::warning: the `.cargo` directory is empty! 
Compiling hello-rust-server v0.1.0 (/Users/Jessica/dev/blog) Finished release [optimized] target(s) in 37.69 secs Running `target\release\hello-rust-server` info:::path: 
`/Users/Jessica/.cargo\bin` ✓ #HelloRustServer v0.1.0 /Users/Jessica/.cargo\bin (built)

We just built and run our Rust application on macOS. If your command doesn't work, you may need to run the cargo --help command for more information about your configuration options for building and running Rust apps, respectively. Alternatively, you can use rust base builder app.

Distributed Systems

Some of the distributed systems that support Rust include:

  1. Rocket supports distributed systems as single nodes or clusters.
  2. Tribolo is a distributed lock service. It solves the problem where multiple processes need to synchronize access to a shared resource, such as file locks or a database connection pool, by allowing them to coordinate through message passing rather than explicit locking. Other programs can send messages to an orchestration program to gain access to the resource controlled by that program.
  3. Charybdis is a distributed storage library for Rust. It provides multiple APIs to access a shared filesystem by partitioning it into smaller "chunks." Any number of read-write-partitioned chunks can be used simultaneously during parallel access, allowing a high degree of concurrency in the interface to them.

Modern web applications are composed of 3 kinds of pieces

Components

Components are decoupled unrelated parts of your app that other components can use. In the example in the previous tutorial, we have a component for dealing with the database; this component does not need to interact with another component to work. Components make it easy to reuse different parts of our code.

Services

A service encapsulates a single responsibility and exposes APIs for other components to call as needed. For example, dealing with the database is just one thing services are responsible for; you can have many other apps that handle different functionality modules like authentication or routing. In our example, we only need one service that handles all database operations.

Application

An application is the top-level component in our architecture and contains all the other components, services, and their relationships to each other. Applications are usually responsible for routing requests, acting as a bridge between the front and back end. Our example has no back end, so we have not dealt with this part.

Rust is a system-level programming language that deals with memory management. It has a rigorous way of writing programs and ensures minimal crashes. Rust is an excellent language for web app development with various libraries like Rocket, Iron web, and Chai. Js is available for a web developer to work with.

Rust's robust typing system prevents errors from occurring and makes catching bugs easier. It also has very compact code that can be understood easily by other developers, which helps maintainability and safety in programming. Rust is ideal for web development since it is scalable and fast, making it efficient enough to handle even the most demanding projects.

Do you find this helpful?