What is up folks? How was your Monday?
Mine started with oversleeping by two hours 🙈 But I made some good progress on a new Python project at work so I'm feeling better about that. Today's blog will likely be a short one as I've just going over data types in the Rust Book. I'll try to summarise any novelties I discover below.
Yesterday's questions answered
- Under the hood, cargo uses
rustdoc
to generate docs. As far as I'm aware, any triple comments///
are considered documentation. Crate docs (//!
) are also included. Side node: there is not only a cargo book (if only there was apip
book) but also a rustdoc book. - Some well known examples of Python-related projects used in Rust are the Python linter ruff and HuggingFace's tokenizers.
Today's open questions
- What is an example of a
const
function - Why would you ever want to handle an integer overflow?
The compiler doesn't get consts
Unlike your average variable, constants in Rust can be declared in the global scope. This comes with a few trade-offs in flexibility:
- Consts are always immutable
- Consts always require a type signature
- Not all expressions can be assigned to consts. Only the expressions in this list can be used.
ScalaRS
Don't get excited functional programmers, I'm not about to tell you about the latest scala ports for Rust 😉 Instead we're focussing on the rudimentary scalar types. One thing I learned today was that an unsigned integer cannot be negative, whereas a signed one can. Signed in this context refers to the +/- sign used to denote an integer's value.
Also when dividing two integers, Rust implicitly uses integer division which, as in Python, truncates the result.
Don't break the flow
If you like to cheap out on assign your programs enough memory to hold integer values, your program may experience what is known as an integer overflow (i.e. when an u8
int is assigned a value > 255). Remember yesterday when I mentioned about the cargo build --release
option. Well as it goes, a production build will not fail in the case of an integer overflow. Instead it will "wrap around" the value such that, for example, a u8
256 will become 1. A debug build will panic
when this happens, however!
Though this debug behaviour implies (and, indeed, Rust book says explicitly) that relying on wrap around behaviour is considered "wrong", the standard library does provide methods for handling such cases. My puny brain can't quite understand why you'd want to do this, so I guess that'll go on the list.
Look, it's a 🦄 ...
One small thing to remember about the char
type in Rust is that it stores values as unicode characters. (Yep, that's it).
Careful how you index
Last but not least today is a gentle reminder from your best friend the Rust compiler that it will not catch Array indexing errors when you use a heap-based integer type (think isize
or usize
).
Top comments (0)