Building a simple Rust webserver

What comes to mind when you think of building a web server with Rust? I bet you think of using Axum, Rocket, Actix, etc., right? These are mature frameworks for building web applications with Rust. By the end of this article, we’ll learn how to build a web server of our own that will receive a get or post request and respond to it without any of those shiny frameworks.…

Making Your Python Code Faster Using Rust

Python is an interpreted and dynamically typed programming language, that has become increasingly popular for many reasons, a few of which include its simplicity compared to other programming languages such as C, C++, and Java. It allows you to accomplish more with less code due to its wide range of libraries. Additionally, it has a very large and supportive community, making it a preferred choice for academic and research work.…

Rust mem::swap Function for Value Swapping

In Rust programming language, memory efficiency, and safety is a feature. The mem::swap function, allows you to swap values between variables efficiently. Understanding mem::swap: In Rust, the std::mem module offers a collection of functions that deal with memory-related operations. Among them, the swap function stands out as a convenient way to exchange the values of two variables. The function is designed to work with mutable references to variables, allowing for…

mem::take Vs mem::replace

mem::take and mem::replace are both used to manipulate ownership and values, but they work slightly differently. mem::take The mem::take method takes ownership of a value and replaces it with a default value of the same type. The original value is returned. Her is a simple example: In the above example, mem::take is used to take ownership of v and replace it with an empty vector and then returning the new…

Mem::replace in Rust

mem::replace is a Rust standard library function that swaps a value in a variable with a new value while returning the original value. In Rust, when you move data out of a container like Vec or Option, the container can no longer be directly used, as its contents have been moved and it is considered empty or invalidated. Imagine a scenario where you want to take the data out of…

Box Smart Pointer in Rust

A pointer in programming is often a piece of data that directs to the location of another piece of data in memory. For example, your home address points to where you live. Smart pointers are data structures that act like pointers but also have additional metadata and capabilities to manage memory automatically and safely. Box smart pointers are one of the many smart pointers in Rust, it allows you to…

Understanding Dereferencing in Rust

Dereferencing is how we access the value that a reference points to. References are like signposts that guide us to the actual data. Imagine them as arrows pointing to a treasure! 💎 Technically, a pointer stores the address of another variable in memory. So, if we do something like the example below: my_ref will hold the address of x in memory. In that case, x is not equal to my_ref, make…

Raw String in Rust

Raw strings are handy when you’re working with content that has characters that would normally require escaping in a regular string. For instance, if you’re dealing with HTML, raw strings can save you from the headache of messing up the original HTML structure. Raw strings starts with r# and closes with a # like so: r#””#. Here is an example of a Javascript code being passed as a string using…

Choosing Between str and String in Rust

In this guide, we’ll explore the reasons for having two types of strings in Rust &str and String , how they work, how they are stored in memory, and when to use each of them. Grab your pop corn and let’s ride along! Why do we have two types of string in Rust? It’s normal for new Rust developers to ask this question: “Why do we have two types of…