diff --git a/README.md b/README.md new file mode 100644 index 0000000..5947119 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# zig 学习指南 + +## [Hello World](./hello/hello.md) + +## [变量](./variable/variable.md) diff --git a/hello/hello.md b/hello/hello.md new file mode 100644 index 0000000..df59275 --- /dev/null +++ b/hello/hello.md @@ -0,0 +1,2 @@ +# Hello + diff --git a/variable/build.zig b/variable/build.zig new file mode 100644 index 0000000..4afc0fe --- /dev/null +++ b/variable/build.zig @@ -0,0 +1,36 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + // 标准构建目标 + const target = b.standardTargetOptions(.{}); + // 标准构建模式 + const optimize = b.standardOptimizeOption(.{}); + // ---------------------------unit test--------------------------------- + // 单元测试步骤构建 + // 添加测试目标 + const test_exe = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + // 执行单元测试 + const run_exe_unit_tests = b.addRunArtifact(test_exe); + // 构建一个 step,用于执行测试 + const test_step = b.step("test", "运行单元测试"); + // 测试 step 依赖上方构建的run_exe_unit_tests + test_step.dependOn(&run_exe_unit_tests.step); + + // -----------------------------run-------------------------------------- + // 添加一个运行目标 + const run = b.addExecutable(.{ + .name = "zig-test", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + // 执行测试 + const run_exe = b.addRunArtifact(run); + // 构建一个 step,用于执行程序 + const run_step = b.step("run", "运行程序"); + run_step.dependOn(&run_exe.step); +} diff --git a/variable/build.zig.zon b/variable/build.zig.zon new file mode 100644 index 0000000..7499c37 --- /dev/null +++ b/variable/build.zig.zon @@ -0,0 +1,67 @@ +.{ + .name = "variable", + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/variable/src/main.zig b/variable/src/main.zig new file mode 100644 index 0000000..02e1020 --- /dev/null +++ b/variable/src/main.zig @@ -0,0 +1,44 @@ +const std = @import("std"); + +/// ## 变量声明 +/// ## 作者 +/// - 邵静 +pub fn declare_variable() void { + var hello: []const u8 = ""; + hello = "Hello, World!"; + std.log.info("{s}\n", .{hello}); +} + +/// ## 常量声明 +/// ## 作者 +/// - 邵静 +pub fn declare_const_variable() void { + const hello: []const u8 = "Hello, World!"; + std.log.info("{s}\n", .{hello}); +} + +/// ## 非符合命名规范的变量声明 +/// ## 作者 +/// - 邵静 +pub fn declare_non_compliant_naming() void { + const @"test non compliant naming" = "Test non compliant naming of variable"; + std.log.info("{s}\n", .{@"test non compliant naming"}); +} + +test "declare variable" { + declare_variable(); +} + +test "declare const variable" { + declare_const_variable(); +} + +test "declare non compliant naming" { + declare_non_compliant_naming(); +} + +pub fn main() !void { + declare_variable(); + declare_const_variable(); + declare_non_compliant_naming(); +} diff --git a/variable/variable.md b/variable/variable.md new file mode 100644 index 0000000..632ea4e --- /dev/null +++ b/variable/variable.md @@ -0,0 +1,47 @@ +# 变量 + +## 变量声明 + +变量声明是声明变量并为其赋值的过程。在zig语言中,变量声明使用关键字`var`,后跟变量名, 变量类型和初始值。 + +```zig +var variable_name: variable_type = initial_value; +``` + +例如,声明一个名为`hello`的变量,类型为`[]const u8`,初始值为`""`: + +```zig +var hello: []const u8 = ""; +hello = "Hello, World!"; + +// 输出 +std.log.info("{s}\n", .{hello}); +``` + +可以看到,我们先是声明了一个`name`,初始值为`""`,然后将`hello`赋值为`"Hello, World!"`,最后再输出hello, 那是因为zig编译器推荐使用常量。 + +## 常量声明 + +常量声明是声明一个不可变的变量,并为其赋值的过程。在zig语言中,常量声明使用关键字`const`,后跟变量名, 变量类型和初始值。 + +```zig +const variable_name: variable_type = initial_value; +``` + +例如,声明一个名为`hello`的常量,类型为`[]const u8`,初始值为`""`: + +```zig +const hello: []const u8 = "Hello, World!"; + +// 输出 +std.log.info("{s}\n", .{hello}); +``` + +## 变量命名 + +zig语言中,变量命名与其它的常见语言类似,必须以字母或下划线开头,后面可以跟任意数量的字母、数字或下划线,且不能与关键字重叠。与其它常见语言不同的是zig允许使用不符合规定的名称,但是必须用`@""`语法。 + +```zig +const @"test non compliant naming" = "Test non compliant naming of variable"; +std.log.info("{s}\n", .{@"test non compliant naming"}); +```