Chapter 3. Fundamental Types
The first principles of the universe are atoms and empty space; everything else is merely thought to exist.
Diogenes Laertes (describing the philosophy of Democritus)
This chapter covers Rust’s fundamental types for representing values: integer and floating-point numbers, strings and characters, vectors and arrays, and so on. These source-level types have concrete machine-level counterparts with predictable costs and performance.
Compared to a dynamically typed language like JavaScript or Python, Rust requires more planning from you up front. You must spell out the types of function arguments and return values, struct fields, and a few other constructs. For the rest, Rust’s type inference will generally figure out the types for you. For example, you could spell out every type in a function, like this:
fngrade_cutoffs(tier_size:u32)->Vec<u32>{letmutcutoffs:Vec<u32>=Vec::<u32>::new();fortierin1u32..5u32{cutoffs.push(100u32-tier*tier_size ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access