【值学习】

This commit is contained in:
邵静 2024-06-05 09:36:08 +08:00
parent e7eb47b255
commit eb44341296
3 changed files with 53 additions and 18 deletions

View File

@ -2,30 +2,31 @@ const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "value",
// run main
const exe_run_main = b.addExecutable(.{
.name = "run main",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
b.installArtifact(exe_run_main);
const run_main_cmd = b.addRunArtifact(exe_run_main);
run_main_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
run_main_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const run_main_step = b.step("run-main", "Run main");
run_main_step.dependOn(&run_main_cmd.step);
const exe_unit_tests = b.addTest(.{
// run main test
const exe_run_main_test = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_exe_unit_tests.step);
const exe_run_main_test_cmd = b.addRunArtifact(exe_run_main_test);
const main_test_step = b.step("run-main-test", "Run main test");
main_test_step.dependOn(&exe_run_main_test_cmd.step);
}

23
value/src/string.zig Normal file
View File

@ -0,0 +1,23 @@
const std = @import("std");
const log = std.log;
const expect = std.testing.expect;
/// ## string测试
/// ##
/// -
pub fn test_string() !void {
const s = "hello world";
const ss: []const u8 = "s";
log.info("s: {s}", .{s});
log.info("ss: {s}", .{ss});
}
pub fn main() !void {
try test_string();
}
test "test string" {
try test_string();
}

View File

@ -46,8 +46,19 @@
通常用于表示简单的数据或特殊的状态。这些基本值在编程中被广泛使用,通常用于控制逻辑、初始化变量或表示特殊的状态。下面是几种常见的基本值及其描述
| 名称 | 描述 |
| ----------------- | ------------------------------------------------------------------ |
| `true``false` | `bool` 值用于表示逻辑真和逻辑假。 |
| `null` | 用于 [`Optioanl`](./optional.md)类型,表示该值为空。 |
| `undefined` | 用于表示值未指定或未定义的状态,常用于初始化变量或表示缺少返回值。 |
| 名称 | 描述 |
| ----------------- | ----------------------------------------------------------------------------------------------- |
| `true``false` | `bool` 值用于表示逻辑真和逻辑假。 |
| `null` | 用于 [`Optioanl`](./optional.md)类型,表示该值为空。 |
| `undefined` | [`undefined`](./undefined.md)用于表示值未指定或未定义的状态,常用于初始化变量或表示缺少返回值。 |
## 字符串与 Unicode 代码字面值
与其它常见语言(`Rust`、`Java`、`C#`、`Go`)不同,在 zig 中没有专门的字符串类型zig中的字符串字面值是常量单项指针指向以空字符结尾的字节数组。
```zig
const hello: ?const[] u8 = "Hello, World!";
log.info("{s}\n", .{hello});
```
字符串字面值的类型编码既包括长度又包括以空字符结尾的事实,因此它们可以被强制转换成切片和以空字符结尾的指针,对字符串字面值进行解引用可以将其转换成数组。