Variables

1

Basic Types, Variables, and Constants

3 minute

The first obstacle for a C programmer in Rust is not ownership. It is being able to write ordinary variables, constants, and basic types without fighting the language.

C code often starts like this:

#include <stdint.h>
#include <stdbool.h>

#define MAX_PACKET_SIZE 1024

int retry = 3;
uint32_t flags = 0;
bool enabled = true;
char tag = 'A';

Rust can express the same ideas, but the defaults are different:

const MAX_PACKET_SIZE: usize = 1024;

let mut retry: i32 = 3;
let flags: u32 = 0;
let enabled: bool = true;
let tag: u8 = b'A';

This article covers only the first layer: basic types, bindings, and constants. Struct layout, arrays, pointers, and strings are separate topics.

Read More