Rust Tutorial – Part 4 – Primitive Data Types


Rust, Tutorials / Tuesday, December 5th, 2017

[This post is part of the series on Rust programming tutorials that I’m writing for enjoyment. All the tutorial links are here.]

Let’s take it step by step. I assume you know what data types are (if not, please run away; Rust should not be your first programming language – it’s far too interesting and powerful for that), but the term primitive might trip you up.

A quick lookup will tell you that the word ‘primitive’ has associated meaning of ‘old-style’, or ‘related to earlier era’, etc. The idea here is that when programming languages just started, they had little more than integers, characters, and booleans, because that was the only immediate needs the language designers could see. As programming evolved later on, having to think of everything as a separate integer became a chore (imagine having to maintain your daily expenses in separate variables – that’s 365 variables for the year. Yikes!), people started designing and use higher-order (read, more useful and more complex) data structures. You might have heard of structures, objects, arrays, lists, hash maps, tuples, etc.

So Rust has primitives. Or, simpler data types. Just four of them – integers, floats, characters and booleans. And since Rust is a systems programming language, it cares about what the size of these types is. That’s because RAM and CPU are at a premium, and if you’re trying to write something like an operating system, you’d better be damned careful with them.

So Rust has several types of integers – 8 bits, 16 bits, 32 bits, and so on; two types of floating point numbers – 32-bit and 64-bit; and so on. I don’t want to bore you with details here. We’ll look at it in more detail when it matters. But for now, please understand that an integer is not just an integer in Rust. Although the examples so far have written stuff like let x = 42; and left everything to the compiler, this sloppiness won’t do for long.

Data types have sizes, and we need to start taking responsibility for them.

[The link to the next tutorial is here.]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.