Rust lang notes - variables and data types
Variables
Variables are defined using the let
keyword
1
| let x = 5; |
By default are inmutables, the way to make a variable mutable is with the mut
keyword.
1 2 3 4 5 6 | fn main() { let mut x = 5; println!("The value of x is: {}", x); x = 6; println!("The value of x is: {}", x); } |
- type def ( explicit / implicit )
In Rust
every variable in Rust has a type, and the compiler can infer the type most of the times. If we want to annotate the type of a variable, we can by putting a colon after the name and then the type after.
1
| let x: i32 = 5; |
Constants
Constants
are declared using the const
keyword ( instead of let
) and the type
must be annotated.
1
| const x: i32 = 5 |
constants
are always immutable and you can not use mut
. You can declared it in any scope, including the global scope, which makes them useful for values that many parts of code need to know about.
Shadowing
Rust
let you declare a new variable with the same name as a previous variable, and the new variable shadows the previous variable.
1 2 3 4 5 | let x = 5; println!("{}", x); let x = x +1; println!("{}", x); |
Shadowing is different from marking a variable as mut
- We get compile errors if we forget the
let
keyword. We can change the type of the value but reuse the same name.
1 2 3 4
let spaces = " "; println!("{}",spaces); let spaces = spaces.len(); println!("{}",spaces);
Data types
Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data.
- Scalar
A scalar type represents a single value.
Rust
has four primary scalar types:integers
,floating-point
( f64 / f32 ),Booleans
, andcharacters
.
Rust
has different types of integers
Length | Signed | Unsigned |
---|---|---|
8-bit | i8 | u8 |
16-bit | i16 | u16 |
32-bit | i32 | u32 |
64-bit | i64. | u64 |
128-bit | i128 | u128 |
arch | isize | usize |
Note on characters
, in Rust
char literals are specified with single quotes, as opposed to string literals, which use double quotes.
1 2 | let c: char = 'A'; let s: String = String::from( "A" ); |
- Compound
Compound types can group multiple values into one type.
Tuples
Tuples let you group multiple values together within parentheses. These multiple values don’t have to be the same type and you can use the index ( with dot notation ) for access the item you want ( starting from 0 ).
1 2 | let my_tuple = ( 1, 'a', false ); println!("{}", my_tuple.1); // will print a |
Arrays
Arrays in Rust are collections where all of the elements have the same type. and a fixed length set when you initialize them.
They can’t get bigger or smaller, even if you declare as mut
.
1
| let fruits = [ "apple", "banana" ]; |
If you need a sequence of values that can change in size, you can use a Vec
type ( provided by the standard library ).
Slice
One primitive type you will see a lot is called a slice. Slices
let us reference a contiguous subset of data in another data structure.
1 2 3 | let numbers = [ 1, 2, 3, 4 ]; let subset = &numbers[0..2]; println!("{}", subset.last().unwrap() ); |
Also posted in collected notes.
Author Javier Viola
LastMod 2020-06-20