C+
Language · View as Markdown

Primitives and literals

Integer and float types

Category Types
Signed i8 i16 i32 i64 isize
Unsigned u8 u16 u32 u64 usize
Float f16 f32 f64

There is no int, no long, no byte. The size is part of the name.

f16 is the IEEE half-precision float (2 bytes), primarily a storage type for ML and graphics data. It converts to and from the wider floats with as (hardware fpext / fptrunc), and exposes a bit-preserving reinterpret to and from its raw u16:

let h: f16 = 1.5f32 as f16;          // fptrunc
let x: f32 = h as f32;               // fpext

let h2: f16 = f16::from_bits(0x3C00 as u16);   // IEEE half 1.0
let raw: u16 = h2.to_bits();

Arithmetic on f16 works, but the idiomatic hot-path pattern is to convert to f32, compute, then convert back.

Other primitives

  • bool: true / false. It cannot be produced by an integer cast.
  • (): the unit type, the implicit return of a function with no arrow.
  • str: a string view (pointer plus length), borrowed. A built-in type.
  • *T: a raw pointer. Operations require unsafe.
  • fn(...) -> R: a function pointer.

The owned, growable string is not a primitive: it is Text, a plain standard-library type (stdlib/text). The compiler knows it only through a single lang-item, so even the string lives in a package. A file that names an owned string, or uses interpolation, imports it:

import "stdlib/text" as text;

let mut s: text::Text = text::from_str("hello");
s.push_str(", world");

.to_text() and string interpolation produce a Text; naming that owned value requires the import (E0613). A single-file program that only needs borrowed views uses str and imports nothing.

Literals

let a: i32 = 42;
let b: u64 = 42u64;            // typed literal
let c: f64 = 3.14;
let d: bool = true;
let e: str = "hello";
let f: i32 = 0x1F;             // hex
let g: i32 = 0b1010;           // binary
let h: i32 = 1_000_000;        // underscore separators

A """...""" literal is a multi-line string, taken verbatim: no indentation stripping and no escape processing, so the bytes between the triple quotes are exactly the value.

let shader: str = """
#version 450
layout(location = 0) in vec3 pos;
void main() { gl_Position = vec4(pos, 1.0); }
""";

Character literals: 'a' is a u8 byte literal, a shorter way to write 97u8. The backslash escapes '\n', '\t', '\r', '\\', '\'', '\"', '\0', and the hex form '\xFF' all work. A multi-byte UTF-8 codepoint such as 'á' is rejected at parse time, since the type is u8, not a full Unicode codepoint. For UTF-8 use a str.