Rust tricks I wish I knew earlier

Manpreet Singh
2 min readNov 22, 2021

Welcome back! Rust is an awesome programming language with a ton of capability, if you’re new to Rust, check out the link below to learn more:

So, let’s talk about some Rust tricks that I wish I had known earlier!

Better Looking Numbers

A super cool trick within Rust is making the numbers look cooler, let’s take a look at the following example:

numberval = 12000000;

This is how most projects actually showcase the numbers, but we can actually substitute the commas with “_”, here is an example:

numberval = 12_000_000;

Swapping

Another awesome trick within Rust is swapping variables, this allows us to swap values with de-initializing either variable. To do this, we can use the swap function, take a look at the following example from Rusts document page:

use std::mem;

let mut x = 5;
let mut y = 42;

mem::swap(&mut x, &mut y);

assert_eq!(42, x);
assert_eq!(5, y);

--

--

Manpreet Singh
Manpreet Singh

Responses (2)