我的个人博客

Home Blog Docs 编程 Zig文档 About
Go to Davirain's GitHub repo Menu

Directory

Back to Menu
Hello World 注释 (Comments) 值 (Values) 字符串字面量 (String Literals) 赋值 (Assignment) 未定义 (undefined) 解构 (Destructuring) Zig 测试 (Zig Test) 整数 (Integers) 浮点数 (Floats) 运算符 (Operators) 数组 (Arrays) 向量 (Vectors) 指针 (Pointers) 切片 (Slices) 结构体 (struct) 枚举 (enum) 联合 (union) 不透明类型 (opaque) 代码块 (Blocks) Switch 语句 (switch) While 循环 (while) For 循环 (for) If 表达式 (if) 推迟执行 (defer) 不可达 (unreachable) 编译时基础 (Basics At Compile-Time) 不返回 (noreturn) 函数 (Functions) 错误处理 (Errors) 可选类型 (Optionals) 类型转换 (Casting) 零位类型 (Zero Bit Types) Void 与结果位置语义 (Void and Result Location Semantics) 编译时 (comptime) 汇编 (Assembly) 原子操作 (Atomics) 异步函数 (Async Functions) 内置函数 (Builtin Functions) 非法行为 (Illegal Behavior) 内存管理 (Memory Management) 编译与源文件 (Compilation & Source) 入口点与标准库选项 (Entry Point & Std Options) Panic 处理器 (Panic Handler) 构建系统 (Zig Build System) C 语言集成 (C) WebAssembly 编译目标 (Targets) 风格指南 (Style Guide) 文档注释指南 (Doc Comment Guidance) 源文件编码 (Source Encoding) 关键字参考 (Keyword Reference) 附录 (Appendix) 容器 (Containers) 语法 (Grammar) Zig 之禅 (Zen) Zig 标准库 (Zig Standard Library) 变量 (Variables) 递归 (Recursion) 生命周期与所有权 (Lifetime and Ownership) 命令行接口 (CLI)

赋值 (Assignment)

使用 const 关键字将值赋给标识符:

const x = 1234;

使用 var 关键字来分配变量:

var y = 5678;

const 和 var 声明都必须有值。如果不想立即给变量一个值,可以使用 undefined。

var z: i32 = undefined;

另请参阅:

  • Variables (变量)
  • undefined
Previous 字符串字面量 (String Literals) Next 未定义 (undefined)

© 2025 Davirain. All rights reserved.

Follow Davirain on Twitter Go to Davirain's GitHub repo