Casts

1

Operators, Expressions, and Casts

5 minute

For C programmers, the tricky part of Rust operators is not whether +, -, *, and / look familiar. The tricky part is that many conversions C performs automatically do not happen in Rust.

C often allows code like this:

#include <stdint.h>
#include <stddef.h>

uint8_t version = 1;
size_t len = 128;
uint32_t total = version + len;

if (total) {
    /* ... */
}

Rust asks you to spell out the type relationship:

let version: u8 = 1;
let len: usize = 128;
let total: usize = version as usize + len;

if total != 0 {
    // ...
}

This article covers operators, expressions, and casts. Control flow itself is a separate topic.

Read More