Zig Data Types
Zig supports multiple data types, including integers, floating-point numbers, booleans, characters, arrays, slices, structs, enums, unions, and pointers.
The following table describes the various data types in Zig:
| Data Type Category | Data Type Examples | Description |
|---|---|---|
| Integer Types | i8, i16, i32, i64, isize | Signed integer type,isizethe size is platform-dependent. |
| Unsigned Integers | u8, u16, u32, u64, usize | Unsigned integer type,usizethe size is platform-dependent. |
| Floating-Point Numbers | f16, f32, f64, f128 | IEEE floating-point types. |
| Boolean Type | bool | Boolean type, with valuestrueorfalse。 |
| Character Type | char | Unicode scalar values. |
| Composite Types | array, vector | Fixed-size arrays and dynamically-sized arrays. |
| Pointer Types | *T, *const T, *mut T | Pointing toTa value of the type,*constwhich is read-only,*mutwhich is mutable. |
| Reference Types | &T, &const T, &mut T | PairTReference to a value of the type,&constwhich is read-only,&mutwhich is mutable. |
| Tuple Types | (T1, T2, ...) | An ordered collection of values with a fixed number and types. |
| Optional Types | ?T | Can benullorTa value of the type. |
| Error Set Types | error{...} | An enum type that contains error values. |
| Function Types | fn(T1, T2, ...) -> R | A function type that accepts parameters and returns a result. |
| Struct Types | struct { ... } | A composite data type containing multiple fields. |
| Enum Types | enum { ... } | A collection of a fixed number of named values. |
| Union Types | union { ... } | A type that can store values of multiple different types, but only one at a time. |
| Alias Types | alias T = U | TYesUAlias of. |
1. Integer Types
Zig provides a variety of integer types, including signed and unsigned integers, ranging in size from 8 bits to 64 bits.
Example
pub fn main() void {
const a: i8 = -128; // 8-bit signed integer
const b: u8 = 255; // 8-bit unsigned integer
const c: i32 = -2147483648; // 32-bit signed integer
const d: u64 = 18446744073709551615; // 64-bit unsigned integer
std.debug.print("a: {}, b: {}, c: {}, d: {}\n", .{a, b, c, d});
}
Zig supports two floating-point types: f32 and f64.
Example
pub fn main() void {
const pi: f32 = 3.14; // 32-bit floating point
const e: f64 = 2.71828; // 64-bit floating point
std.debug.print("pi: {}, e: {}\n", .{pi, e});
}
3. Boolean Type
The boolean type is represented by bool, and its values can be true or false.
Example
pub fn main() void {
const is_true: bool = true;
const is_false: bool = false;
std.debug.print("is_true: {}, is_false: {}\n", .{is_true, is_false});
}
4. Character Type
The character type uses u8 to represent a single character.
Example
pub fn main() void {
const letter: u8 = 'A';
std.debug.print("letter: {}\n", .{letter});
}
5. Arrays and Slices
Arrays are fixed in size, while slices are dynamically-sized arrays.
Example
pub fn main() void {
const array: [5]i32 = [5]i32{1, 2, 3, 4, 5}; // Fixed-size array
const slice: []const i32 = array[1..4]; // Slice
std.debug.print("array: {}, slice: {}\n", .{array, slice});
}
6. Structs
Structs are defined with struct, allowing you to create complex data types.
Example
const Point = struct {
x: i32,
y: i32,
};
pub fn main() void {
const p = Point{ .x = 10, .y = 20 };
std.debug.print("Point: ({}, {})\n", .{p.x, p.y});
}
7. Enums
Enums are defined with enum, allowing you to create types with named values.
Example
const Color = enum {
Red,
Green,
Blue,
};
pub fn main() void {
const color: Color = Color.Green;
std.debug.print("Color: {}\n", .{color});
}
8. Unions
Unions are defined with union, allowing you to create a variable that can store values of different types.
Example
const Number = union(enum) {
Int: i32,
Float: f32,
};
pub fn main() void {
const num: Number = Number{ .Int = 10 };
switch (num) {
Number.Int => std.debug.print("Integer: {}\n", .{num.Int}),
Number.Float => std.debug.print("Float: {}\n", .{num.Float}),
}
}
9. Pointers
Pointers are defined with*and can point to variables of a specific type.
Example
pub fn main() void {
var a: i32 = 10;
const p: *i32 = &a; // Pointer to a
std.debug.print("Value: {}, Pointer: {}\n", .{a, p.*});
}