Are numbers that complex?

In rust they are more complicated because rust needs to know the size.

















Numbers in typescript!

// We would call this an integer
4 // this... is technically a smi, but for your purpose, its a number
// integer becomes a float auto_magically_ in js
4 / 3 = 1.3333333333333333
// this is totally cool
4 * -1 = -4
















None of that was cool for rust

Rust you have to specify the types <NUMBER> = power of two

i<NUMBER> = an integer that can be negative or positive (signed)

u<NUMBER> = an integer that can be positive only (unsigned)

f<NUMBER> = a number that requires decimal point

usize = a u<NUMBER> where <NUMBER> is your system arch. (64bit = u64)

isize = a i<NUMBER> where <NUMBER> is your system arch. (64bit = i64)

4 / 3 = 1
// cannot divide {float} by {integer}
// yes... this is an error
4.0 / 3 = Nope
// 4 is an i32
4 * -1 = -4
// 4 is an i32
let foo = 4u32; // saying its a 4 that is a u32 (defining type)
foo * -1 // ERROR

If you have ever worked with any static language, this should be pretty straight forward.

















The difference between String and &str

Yes, you will see there are two types of strings you commonly run into. So what are they?


String

  • Well String is a heap allocated (heap may be a foreign word to you)
  • String can be mutable

&str

  • this points to a sequence of utf-8 characters. Its commonly called a slice. Its a view into a String
  • its immutable
  • its analogous to &[u8]

So if i say String, i mean String and if i say stir i mean &str (quick whiteboarding)