Understanding Closures in Rust

Like several other languages, Rust programming language supports closures also known as anonymous functions or lambda functions which is a powerful feature used for varying reasons depending on who you ask. We’ll break down closures in Rust in this article, so you can start using it in your Rust code immediately and properly. A closure in Rust is a function without a name that can be used as a variable.…

Rust’s Newtype Pattern: Adding Type Safety and Clarity

The newtype pattern is a Rust idiom that can be used to add type safety and clarity to code. At its core, the newtype pattern involves wrapping an existing Rust type (like a number or a string) inside a new struct. This might seem unnecessary at first, but the magic is that this new struct becomes a distinct type and ensures that you are using the correct type where necessary.…

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.…

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::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

Box smart pointer is a pointer in Rust that allows you to allocate memory on the heap, even though the Box pointer itself exists in the stack. Key notes: To create a Box, you can use the Box::new() function. This function takes the value that you want to store on the heap and returns a Box that points to the allocated memory. You can access the data stored in the…

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…

Choosing Between &str and String in Rust

In Rust, handling text is a common task, and understanding when to use `&str` and when to use `String` is key — it could lead to more efficient code when done right or otherwise when misused. When you have passed the stage of just getting your Rust code to work you want to make it more efficient. And you want to understand how string really works. While Rust has only…

Rust Lifetimes Simplified

This guide is an attempt to simplify Rust Lifetimes, it’s a series and will be divided into several chapters. We’ll start with chapter one today. Chapter 1 Lifetimes and its benefits In Rust programming, lifetimes is a critical yet misunderstood concept. Assuming we are not talking about programming, when we talk about lifetimes what comes to mind? According to Oxford Dictionary, a lifetime is the duration of a person’s life…