Zigデータ型
Zigは、整数、浮動小数点数、ブール値、文字、配列、スライス、構造体、列挙型、共用体、ポインターなど、多様なデータ型をサポートしています。
以下の表は、Zigにおける各データ型の説明です。
| データ型のカテゴリ | データ型の例 | 描述 |
|---|---|---|
| 整数类型 | i8, i16, i32, i64, isize | 符号付き整数型、isizeプラットフォーム依存のサイズです。 |
| 无符号整数 | u8, u16, u32, u64, usize | 符号なし整数型、usizeプラットフォーム依存のサイズです。 |
| 浮点数 | f16, f32, f64, f128 | IEEE浮動小数点型。 |
| 布尔类型 | bool | ブール型、値はtrue或false。 |
| 字符类型 | char | Unicodeスカラー値。 |
| 复合类型 | array, vector | 固定サイズ配列と可変サイズ配列。 |
| 指针类型 | *T, *const T, *mut T | 指向T型の値を指すポインタ、*const読み取り専用で、*mut可変です。 |
| 引用类型 | &T, &const T, &mut T | に対してT型の値への参照、&const読み取り専用で、&mut可変です。 |
| 元组类型 | (T1, T2, ...) | 固定された個数と型の値を含む順序付けられた集合。 |
| 可选类型 | ?T | であり得るnull或者T类型值。 |
| エラー集合型 | error{...} | エラー値を含む列挙型。 |
| 函数类型 | fn(T1, T2, ...) -> R | 引数を受け取り結果を返す関数型。 |
| 结构体类型 | struct { ... } | 複数のフィールドを含む複合データ型。 |
| 枚举类型 | enum { ... } | 固定数の名前付き値の集合。 |
| 联合体类型 | union { ... } | 異なる複数の型の値を格納できる型ですが、同時に格納できるのは1つの値だけです。 |
| 别名类型 | alias T = U | TであるUの別名。 |
1、整数型
Zigは、符号付きおよび符号なし整数を含む複数の整数型を提供しており、サイズは8ビットから64ビットまでさまざまです。
实例
const std = @import("std");
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});
}
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は、f32 と f64 の2つの浮動小数点数型をサポートしています。
实例
const std = @import("std");
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});
}
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、ブール型
ブール型は bool で表され、値は true または false を取ります。
实例
const std = @import("std");
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});
}
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、文字型
文字型は u8 を使用して単一の文字を表します。
实例
const std = @import("std");
pub fn main() void {
const letter: u8 = 'A';
std.debug.print("letter: {}\n", .{letter});
}
pub fn main() void {
const letter: u8 = 'A';
std.debug.print("letter: {}\n", .{letter});
}
5、配列とスライス
配列は固定サイズで、スライスは動的サイズの配列です。
实例
const std = @import("std");
pub fn main() void {
const array: [5]i32 = [5]i32{1, 2, 3, 4, 5}; // 固定大小数组
const slice: []const i32 = array[1..4]; // 切片
std.debug.print("array: {}, slice: {}\n", .{array, slice});
}
pub fn main() void {
const array: [5]i32 = [5]i32{1, 2, 3, 4, 5}; // 固定大小数组
const slice: []const i32 = array[1..4]; // 切片
std.debug.print("array: {}, slice: {}\n", .{array, slice});
}
6、结构体
構造体は struct で定義され、複雑なデータ型を作成できます。
实例
const std = @import("std");
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});
}
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、枚举
列挙型は enum で定義され、名前付きの値を持つ型を作成できます。
实例
const std = @import("std");
const Color = enum {
Red,
Green,
Blue,
};
pub fn main() void {
const color: Color = Color.Green;
std.debug.print("Color: {}\n", .{color});
}
const Color = enum {
Red,
Green,
Blue,
};
pub fn main() void {
const color: Color = Color.Green;
std.debug.print("Color: {}\n", .{color});
}
8、联合体
共用体は union で定義され、異なる型の値を格納できる変数を作成できます。
实例
const std = @import("std");
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}),
}
}
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、指针
指针用*定義であり、特定の型の変数を指すことができます。
实例
const std = @import("std");
pub fn main() void {
var a: i32 = 10;
const p: *i32 = &a; // a へのポインタ
std.debug.print("Value: {}, Pointer: {}\n", .{a, p.*});
}
pub fn main() void {
var a: i32 = 10;
const p: *i32 = &a; // a へのポインタ
std.debug.print("Value: {}, Pointer: {}\n", .{a, p.*});
}