Vaultree
Published in

Vaultree

Variables and Primitive Data Types in Rust

In part 1 of this Vaultree Series on Rust, we learnt how to start a project using Rust. Now we will cover variables and data types in depth.

Variables, Mutability, and Immutability

Variables are spaces in computer memory used to store certain data. We can think of them as if they were “boxes” to store some item.

let x = 10;
let y: f64;
y = 17;
let x = 10;
x = 17;
let mut x = 10;
x = 17;

Constants

In addition to variables we can create constants. Constants will always be immutable, and to create constants we use the keyword “const” instead of “let”, and the type needs to be specified.
Another difference with constants is that they can be declared in any scope, including the global scope. They cannot be the results of functions or some value that could only be calculated at runtime.

Shadowing

In Rust, it is possible to declare a new variable using the same name that was already used before, and this new one overshadows the previous variable, being able to change even its type. Let’s create a project to exemplify, enter the directory and run VS Code:

cargo new variables
cd variables
code .
const PI: f64 = 3.14159;
fn  main() {
let mut x = 10;
println!("The value of x is {}", x);
x = 17;
println!("The value of x is {}", x);
let x = "test";
println!("The value of x is {}", x);
println!("The value of PI const is {}", PI);
}
Compiling variables v0.1.0 (/home/desktop/dev/workspace/rust)
Finished dev [unoptimized + debuginfo] target(s) in 0.25s
Running `target/debug/variables`
The value of x is 10
The value of x is 17
The value of x is test
The value of PI const is 3.14159

Primitive Data Types

Rust has a list of data types that are considered primitive. Rust primitive types can be grouped into scalar and composite data types.
What are primitive data types?
Primitive data types, as the name implies, come with a programming language. They are built-in and, when combined, can form more complex data types, called non-primitive data types.
As we mentioned, the Rust programming language comes with a list of built-in primitive data types that developers can use as building blocks for other data types.

Primitive data types in Rust

We want to group them first into scalar and composite data types. The difference between these two is that composite types contain multiple values ​​in one type whereas scalar types only contain one value.

Primitive Scalar Types in Rust

There are four scalar primitive types that you should be familiar with in Rust:

let active = true;
let inactive = false;
let first = 'a';
let second = 'b';
let symbol = '∞';
let height = 189; //u8
let weight = 78; // u8
let size = 54; // u8
let data = -455// i8
f32 ---> -3.8x10^8 to +3.8x10^8
f64 ---> -1.8x10^308 to +1.8x10^308
let interest = 1.20;
let returns = 2.80;
let agency = 10.0;

Composite Primitive Types in Rust

Array data type

let name = String::new();
name.push_str = 'Pedro Aravena';
println("{}", name);
let name:&str = 'Pedro Aravena';
let company:&str = 'Vaultree';
let grades: [132:6] = [20, 10, 30, 40, 50, 10, 20];
let slice = &[20...4]; // 20, 10, 30, 40
let employee: (&str, i32, &str) = ('Pedro Aravena', 25, 'Developer Advocate');

Conclusion

Understanding how to work with all the different types of primitive data in Rust is extremely helpful. We’re here to support your Rust journey. If you have any questions, please drop them below and stay tuned for upcoming articles on functions and flow controls.About Vaultree

--

--

Vaultree’s Encryption-in-use enables businesses of all sizes to process (search and compute) fully end-to-end encrypted data without the need to decrypt.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vaultree Tech Community

Vaultree is a revolutionary encryption startup allowing processing of fully encrypted data at run-time. Let’s create an encrypted tomorrow…together!