Rust datatypes: Difference between revisions

From wikinotes
Line 2: Line 2:
<blockquote>
<blockquote>
<syntaxhighlight lang="rust">
<syntaxhighlight lang="rust">
'a'   // char
'a'       // char
"abc" // string
"abc"     // string
1234   // i32
1234       // i32
3.14   // f32
3.14       // f32
true/false // bool
</syntaxhighlight>
</syntaxhighlight>



Revision as of 02:28, 1 September 2021

Literals

'a'        // char
"abc"      // string
1234       // i32
3.14       // f32
true/false // bool
1_000      // == 1000
1.000_000  // == 1.000000

Primitives

Text

string

"abcd"  // string

char

char
'a'    // character (single)

Numbers

implied type let var = 12;
assigned type let var: i8 = 12;
type suffix let var = 12i8;

Integers

  • signed integers range is split in two, can be positive/negative
  • unsigned integers are positive, and use all available bits
  • use radix to calculate max size that can be accomodated with b bits
// signed integers, by bit-size
i8   //        -128..127
i16  //      -32768..32767
i32  // -2147483648..2147483647
i64  // ...
i128
isize

// unsigned integers, by bit-size
u8   // 0..255
u16  // 0..65535
u36  // 0..4294967295
u64  // ...
u128
usize

Floating Point

f32
f64

Boolean

true
false

Collections

tuples

arrays