Here are a couple of things Rustonomicon says are undefined behavior:
- Reading uninitialized memory
- Producing invalid primitive values:
- dangling/null references
- a bool that isn't 0 or 1
- an undefined enum discriminant
- a char outside the ranges [0x0, 0xD7FF] and [0xE000, 0x10FFFF]
- A non-utf8 str
Notice it says "reading uninitialized memory", not "producing uninitialized memory" and also that it says "producing invalid primitive values", not "reading invalid primitive values". I interpret this so that it's undefined behavior to create an invalid primitive value even if you never read the value in its invalid state. Please clarify if I'm interpreting this correctly or not.
For example, the following things would be considered undefined behaviour according to my interpretation of the Rustonomicon:
// May produce an invalid char value
let mut a: char = unsafe { std::mem::uninitialized() };
let a = 'A';
enum Enum { Foo, Bar }
// May produce an invalid Enum value
let mut e: Enum = unsafe { std::mem::uninitialized() };
e = Enum::Foo;