6.6.2.5. Null data types¶
6.6.2.5.1. None/NULL¶
Many programming languages, but not all, have a data type of None. This is the equivalent of blank entry. We might have code along the lines of
exam_mark = None
because we’ve not got the exam mark available yet. The blank may be filled in later in the code. It’s useful to set data to None if we don’t have it yet, because we can then check, for example at the end of the code. Any students who have None as their exam mark didn’t sit the exam. (Or there’s any error in our code!)
In C/C++ NULL is used to the same effect.
It’s useful to have a dedicated data type to represent missing data, rather than using something like 0 or an empty string "".
6.6.2.5.2. Some¶
Rust introduces a Some type. This lets you check that you have got something, without necessarily caring what it is at that point in time. It might be an integer, or a string or something else. Some just lets you represent that you have something present. This can be very helpful when you have multiple data types that a function might work with.
6.6.2.5.3. NaN¶
NaN stands for Not a Number. This is a special floating point value defined in the IEEE 754 floating point standard. It is used to represent missing or undefined numerical data. For example, if we try and compute \(0/0\), the result is NaN. If you’re doing a sum, and the result isn’t value for some reason, you’ll often get NaN as the result.
EEEN11202 course notes