[go: up one dir, main page]

0% found this document useful (0 votes)
16 views1 page

Primitives - Rust by Example

Rust offers a variety of primitive types, including scalar types like signed and unsigned integers, floating points, characters, booleans, and the unit type. It also includes compound types such as arrays and tuples, with variables capable of type annotations and type inference. The document illustrates variable declaration and type assignment, emphasizing the mutability and type constraints in Rust.

Uploaded by

victinhus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Primitives - Rust by Example

Rust offers a variety of primitive types, including scalar types like signed and unsigned integers, floating points, characters, booleans, and the unit type. It also includes compound types such as arrays and tuples, with variables capable of type annotations and type inference. The document illustrates variable declaration and type assignment, emphasizing the mutability and type constraints in Rust.

Uploaded by

victinhus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Primitives

Rust provides access to a wide variety of


primitives . A sample includes:

Scalar Types

signed integers: i8 , i16 , i32 , i64 , i128


and isize (pointer size)
unsigned integers: u8 , u16 , u32 , u64 ,
u128 and usize (pointer size)
floating point: f32 , f64
char Unicode scalar values like 'a' , 'α'
and '∞' (4 bytes each)
bool either true or false
and the unit type () , whose only possible
value is an empty tuple: ()

Despite the value of a unit type being a tuple, it is


not considered a compound type because it does
not contain multiple values.

Compound Types

arrays like [1, 2, 3]


tuples like (1, true)

Variables can always be type annotated. Numbers


may additionally be annotated via a suffix or by
default.
   Rust
Integers By
default to Example
i32 and floats to f64.
Note that Rust can also infer types from context.

1 fn main() {   
2 // Variables can be type annotated.
3 let logical: bool = true;
4
5 let a_float: f64 = 1.0; // Regular a
6 let an_integer = 5i32; // Suffix an
7
8 // Or a default will be used.
9 let default_float = 3.0; // `f64`
10 let default_integer = 7; // `i32`
11
12 // A type can also be inferred from c
13 let mut inferred_type = 12; // Type i
14 inferred_type = 4294967296i64;
15
16 // A mutable variable's value can be
17 let mut mutable = 12; // Mutable `i32
18 mutable = 21;
19
20 // Error! The type of a variable can'
21 mutable = true;
22
23 // Variables can be overwritten with
24 let mutable = true;
25 }
26

See also:

the std library, mut , inference , and shadowing

 

You might also like