Expertise
8 mins read

Rust: the future of web development?

Development
Senior PHP Developer
Development

Senior PHP Developer

Many companies have found use cases for Rust.

The types of projects it has been used for include CLI applications, web services, dev ops tools, embedded systems, crypto, bioinformatics, search engines and machine learning.

A notable example of its use is in the codebase for the Firefox browser.

Large companies such as Amazon, Dropbox, Facebook and Microsoft have used Rust to achieve faster performance whilst ensuring the code is memory safe. As an article showing the use of Rust by companies states:

 

“In the future, expect Rust usage to increase as more and more companies discover how it can improve their codebases.” – Serokell

Stack Overflow Insights

“Rust is on its seventh year as the most loved language with 87% of developers saying they want to continue using it. Rust also ties with Python as the most wanted technology with TypeScript running a close second.”

This shows us that Rust is a language that continues to be preferred by developers over many years, meaning that it is not just a momentary craze. Being one of the most wanted technologies, it is likely where the industry moves to next. 

“Similar to last year we see over 10k Javascript developers that want to start or continue developing in Go or Rust.”

This means that there are a large number of developers who believe Go or Rust is the future of web development and are keen for it to be introduced into more codebases. Being that it was JavaScript developers in particular who answered this in the survey would suggest that this is seen as a language to be used for the web.

Web­Assembly

Rust can use Web­Assembly as its compile target, which opens up a lot of possibilities for using Rust in the frontend. A suggested use for this is to replace some of the Javascript code with Rust code to improve performance.

There is a brilliant guide on what to use Rust and WebAssembly for and how to implement it here: Rust and WebAssembly

Correctness Guarantees

 

Memory safety without garbage collection

Through the principles of ownership and borrowing, Rust is able to stay memory safe without the costly overhead of a garbage collector. The strict rules on the lifecycle of a value means that once its owner is no longer in scope, it is automatically removed from the memory without a garbage collector needing to run scheduled checks for values no longer required.

 

Concurrency without data races

This means that multiple threads in a process can run at the same time without worrying over which thread will access a value first and if that value might change in the time between the two threads accessing it.

There are two of Rust’s ownership rules that achieve this:

  • There may exist at most one mutable reference to a value.

OR

  • There may be any number of immutable references to a value and while they exist the value may not be mutated.

 

These rules mean that if a value could be changed, then it cannot be read by any references other than its original self. Likewise, if there are any read only references to the value then the original value cannot be changed whilst these still exist.

This is a concept not found in other languages which allows Rust to check for any potential issues at compile time so that it can be fixed before the program is run.

For an example of where this has been applicable see this post about how it benefited the developers of Firefox: Fearless Concurrency in Firefox Quantum.

 

Performance without compromising safety

People really enjoyed being able to aggressively write code without having to worry about safety. Rust is able to guarantee memory safety whilst being one of the most performant programming languages available.

Rust: The Programming Language for Safety and Performance

 

Easy Error Messages

The Rust compiler returns build errors that are very easy to understand and give hints on what you may need to change. Because Rust works differently to many common languages, this can make learning Rust a lot easier. A crate called Clippy extends this functionality to provide over 550 lints: Clippy

 

Screenshot_2022-10-28_at_15.01.23

 

Noncommercial and open source

This means that there are no worries that the language may introduce paid versions which could cause unexpected plan changes in the future. It also has the benefit of allowing a large group of developers to contribute to the code and check what others have done: All-time Rust Contributors

Why Not

Learning curve is big

The approach which Rust takes to ownership of values can take some time to learn. Especially if you are coming from a language such as JavaScript or PHP which does not have this. There are many other changes to understand such as using macros to achieve common functionality instead of functions and often needing to use vectors instead of arrays to prevent mutability issues.

 

Very explicit

As a low level language, you will often need to implement your own code for what are simple commands in higher level languages. This can be overcome to some extent by the use of macros made by other developers. This also means that you have more control over how that specific functionality works.

 

Time to code is longer

Due to the above, the time it takes to make the complete code may be greater than if a higher level language such as JavaScript or PHP are used. However, this time may be worthwhile as the code will work exactly the way you want it to and the improved performance gained compared to these languages.

 

Compile times can be long

Rust has been identified as being slow to compile. There are some articles which suggest this may be because of the way the code was written rather than a flaw in the Rust language itself. There are guides available to help improve the compilation times but it still may not be as fast as some other compiled languages: Fast Rust Builds

A picture of two computer screens with Rust development language on them.

An In Depth Look

Differences to Other Languages

The following are some examples of the differences you will find moving from a high level web language such as PHP to Rust.

 

General Purpose

Languages such as PHP are primarily designed for implementing websites. However, Rust is a general purpose language which can be used in many different places. Whilst with PHP you are limited to the options available in the PHP interpreter and the web server, in Rust you are able to do anything if the time is spent to implement it.

 The following article sums up the differences between PHP to Rust in one sentence:

“From being [in] taxis to travel around the city, to your own car for going anywhere.” – From PHP to Rust

 

Ownership and Borrowing

One of the core principles of values in Rust is that values can only be owned by one variable. Consider the example below:

Screenshot_2022-10-28_at_15.09.07

The above code will fail on line 4 because ownership of the value was passed to ‘bob’ rather than shared with it. Therefore, ‘alice’ no longer has a value assigned to it.

The same is also true with how values are used in functions. If a variable is passed as a parameter to a function without the reference symbol ‘&’ then the function takes ownership of the value and the original variable no longer references that value.

Whilst this initially takes some getting used to, it helps the developer keep track of exactly what is happening to each value and where it is used. This is also one of the reasons why rust does not need a garbage collector.

 

Statically and strongly typed

Statically typed means that the type of all variables is known by the compiler before the program has even started. This is different to many languages where guesses will be made about the values data type at run time.

Strongly typed means that the language includes a set of core data types and every variable must be described by using these types.

 

Compiled instead of interpreted

Unlike languages such as JavaScript and PHP which are read by another process to interpret their instructions into actions, Rust is compiled into the machine’s code so that it can be run stand-alone without another process to understand it.

 

Extremely fast and reliable

One of the key features of Rust is its speed. Because it is compiled and does not have lots of overheads such as garbage collection, it is able to run as fast as possible.

Because of the above features, if the code compiles without errors, you can be assured that the program will not encounter any programmatic errors on execution.

 

Cargo package manager

Whilst developers using PHP can opt for Composer as a package manager, it is not a fundamental part of the PHP language. Cargo, on the other hand, is the package manager for Rust and is included with Rust on a standard installation. Installing Rust using rustup will also install cargo.

 

Macros for functionality

Unlike most languages where all actions that can be performed are functions, Rust allows the use of macros in place of code that would perform those actions. A macro instructs the compiler to replace the macro reference with the code of the macro.

Because running a macro essentially inserts extra code in its place, it can be used for more than just functions. For example, the ‘vec’ macro allows you to create a vector simply by using the following:

Screenshot_2022-10-28_at_15.12.54

Recommended Resources:

Free learning resources are available on the Rust language website: https://www.rust-lang.org/learn

Rustlings: Small exercises to get you used to reading and writing Rust code! is particularly good as an interactive way to learn Rust.

A half-hour to learn Rust is a quick guide for if you need to pick up the basics fast.

Rust Playground is an example of a project written in Rust and React.

Crates.io provides information on crates available to extend your codebase and is written in Rust [GitHub - rust-lang/crates.io].

 

Related articles

Rawnet empowers clients to drive change and stay competitive and innovative.

In today’s digital age, where web applications and software dominate the tech landscape.

Go, or GoLang, is favoured in DevOps for its exceptional performance.

Other articles by Elliot Dickerson