Reader small image

You're reading from  Rust for Blockchain Application Development

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781837634644
Edition1st Edition
Concepts
Right arrow
Author (1)
Akhil Sharma
Akhil Sharma
author image
Akhil Sharma

Akhil Sharma is a Software Engineer and an entrepreneur. He is the CTO of Dominate, a SaaS product company, and the founder of Myrl Tech, a tech services company providing technology consulting to some of the top enterprise companies in the world such as HP, 3M, Honda, Bose, and Adobe. He has 12+ years of industry experience and a solid understanding of building digital products. He is adept at multiple programming languages such as Golang, Rust, Ruby, Python, and JavaScript. He has mentored hundreds of engineers offline and discovered that they knew the programming language and the concepts around it but were unable to use them in real-world applications. He aims to close this gap by teaching how to apply the concepts practically and build projects in real time.
Read more about Akhil Sharma

Right arrow

Rust – Necessary Concepts for Building Blockchains

Even though Rust is a new programming language, it’s gaining popularity quickly since it makes the job of the programmer simple. With Rust, you get a simple promise – if your program passes the compiler’s checks, it is most likely free of undefined behavior, in the sense that this reduces the chances of encountering unexpected bugs. However, it’s important to note that no compiler can guarantee absolute freedom from all unexpected behaviors, especially in complex domains such as asynchronous and embedded code.

Rust is renowned for its speed and efficiency, often drawing direct comparisons with C and C++. It holds significant advantages over these languages, largely due to the proactive enforcement of rules by the Rust compiler. Unlike C and C++, where a multitude of rules exist and the onus is on the programmer to adhere to them, Rust assumes a more active role in rule enforcement. This fundamental...

Introducing Rust

Many blockchains have selected Rust as their go-to programming to write their core protocol on which the rest of the architecture is built. There are plenty of reasons for this and since our book is about Rust for blockchain development, we need to understand why Rust is so popular for the blockchain use case.

In the following subsections, our focus will be on understanding the reasons why Rust is a perfect fit for blockchains.

The benefit of being statically typed

Rust adheres to static typing principles, requiring an explicit declaration of variable types, which are resolved during the compilation process. This allows the compiler to check if a variable can do what it’s supposed to, protecting against errors when the program runs.

In statically typed languages, the result or end product usually takes the form of a lower-level representation. Pre-compilation, the compiler possesses assurances for correctness and consistency regarding the structure...

Rust’s advantage for blockchains

Blockchains and blockchain-related technologies that use Rust have an edge over others and this section is dedicated to exploring this aspect. Let’s learn about how these technologies benefit from using Rust.

Blockchains that use Rust

Some of today’s most popular blockchains, such as Solana, NEAR, and Polkadot, use Rust primarily. Polkadot even has a framework called Substrate that can be used to build new blockchains (we have a chapter dedicated to it – that is, Chapter 10, Hands-On with Substrate, and this is the framework that was used to build Polkadot itself.

Many new, highly innovative blockchains such as Aptos and Sui also use Rust. In Chapter 1, Blockchains with Rust, we learned how Aptos uses parallel processing at the Layer 1 level to make the network extremely scalable.

Hyperledger’s Sawtooth is an open source, enterprise-ready blockchain solution for building, deploying, and running distributed...

Learning basic Rust concepts

While Rust is quite an extensive language and to cover it completely requires a book of its own, you are advised to supplement your learning with a dedicated Rust book. You can find some examples at https://doc.rust-lang.org/book/. However, the most important concepts that we will often require when working with blockchains can be covered quickly. This is what we will attempt to do here.

First, let’s talk about variables and constants. These are the basic building blocks of any programming language.

Variables and constants

Variables are crucial to Rust as they are values that may change multiple times throughout the lifetime of the program. As can be seen in the following example, defining variables in Rust is extremely simple:

fn main() {
   let x = 5;
   println!("The value of is:{x}");
}

In the preceding code, we assign a value of 5 to x, where x is the variable, after which we print out the...

Exploring intermediate Rust concepts

In the previous section, we understood a lot of the foundational concepts of Rust. This will help us in reading and deciphering larger programs that we shall encounter in the later chapters of this book. Now, it’s time to build upon the knowledge that we’ve acquired to start understanding slightly more advanced Rust concepts so that we gain a stronger grip on the concepts and deploy them in real-world scenarios.

Control flow

All programming languages have a way to execute different pieces of code that are dependent on meeting a condition. This execution happens with the help of branches, which are usually defined with the help of if, else, and else if. In Rust, we have similar concepts. So, let’s learn about these with the help of some examples:

fn main() {
   let i = 5;
   if i > 3 {
      println!("condition met, i is greater than 3");
 ...

Delving deep into advanced Rust concepts

Now that we have built upon our foundational knowledge of Rust, it’s time to expand our bases with some more concepts that will make us even more comfortable with Rust and let us build real-world projects with it.

In the following subsections, we will learn about concepts that unlock some advanced functionality for us, such as hashmaps, ownership, borrowing, crates, modules, and cargo. These help us work smoothly with slightly bigger projects. However, you may end up using these features more often than you expect, so it’s important to pay close attention to these subsections.

Hashmaps

Hashmaps in Rust, as in many other programming languages, are collections of key-value pairs. They are especially efficient for scenarios where you need to quickly look up data using keys rather than index values. The efficiency of hashmaps comes from their use of a hashing function, which converts keys into hash codes. These hash codes...

Summary

In this chapter, we explored why Rust stands out as one of the most favored programming languages, particularly due to its exceptional speed and performance. Moreover, its distinctive attributes, such as type safety, AOT compilation, zero-cost abstractions, and freedom from garbage collection, contribute to its popularity. Following this, we’ve delved into crucial Rust concepts that lay the foundation for upcoming chapters. These concepts encompass variables, data types, tuples, arrays, slices, control flow, functions, and vectors, all of which will be instrumental in our continued learning journey.

We also covered advanced concepts such as hashmaps, ownership and borrowing, crates, modules, and cargo, all of which help us work with projects.

This was a quick, whirlwind tour of the most important and the most commonly used Rust features that we will be using and reusing throughout the rest of this book, hence why it was important to get this out of the way. However...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Rust for Blockchain Application Development
Published in: Apr 2024Publisher: PacktISBN-13: 9781837634644
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Akhil Sharma

Akhil Sharma is a Software Engineer and an entrepreneur. He is the CTO of Dominate, a SaaS product company, and the founder of Myrl Tech, a tech services company providing technology consulting to some of the top enterprise companies in the world such as HP, 3M, Honda, Bose, and Adobe. He has 12+ years of industry experience and a solid understanding of building digital products. He is adept at multiple programming languages such as Golang, Rust, Ruby, Python, and JavaScript. He has mentored hundreds of engineers offline and discovered that they knew the programming language and the concepts around it but were unable to use them in real-world applications. He aims to close this gap by teaching how to apply the concepts practically and build projects in real time.
Read more about Akhil Sharma