【值学习】添加值学习的内容,包括基本数据类型、基本值等内容
【md语法优化】优化了variable.md中的一些语法问题,如删除行尾:等
This commit is contained in:
parent
b2ba7d2a4f
commit
e7eb47b255
|
@ -2,4 +2,6 @@
|
|||
|
||||
## [Hello World](./hello/hello.md)
|
||||
|
||||
## [值](./value/value.md)
|
||||
|
||||
## [变量](./variable/variable.md)
|
||||
|
|
31
value/build.zig
Normal file
31
value/build.zig
Normal file
|
@ -0,0 +1,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",
|
||||
.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());
|
||||
if (b.args) |args| {
|
||||
run_cmd.addArgs(args);
|
||||
}
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_unit_tests = 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);
|
||||
}
|
67
value/build.zig.zon
Normal file
67
value/build.zig.zon
Normal file
|
@ -0,0 +1,67 @@
|
|||
.{
|
||||
.name = "value",
|
||||
// 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 <url>` 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",
|
||||
},
|
||||
}
|
65
value/src/main.zig
Normal file
65
value/src/main.zig
Normal file
|
@ -0,0 +1,65 @@
|
|||
// 顶级声明不受顺序影响
|
||||
const std = @import("std");
|
||||
const os = std.os;
|
||||
const assert = std.debug.assert;
|
||||
const log = std.log;
|
||||
|
||||
/// ## 测试int类型
|
||||
/// ## 作者
|
||||
/// - 邵静
|
||||
fn test_integer_variables() void {
|
||||
const one_plus_one: i32 = 1 + 1;
|
||||
log.info("1 + 1 = {}\n", .{one_plus_one});
|
||||
assert(one_plus_one == 2);
|
||||
}
|
||||
|
||||
/// ## 测试float类型
|
||||
/// ## 作者
|
||||
/// - 邵静
|
||||
fn test_float_variables() void {
|
||||
const seven_div_three: f32 = 7.0 / 3.0;
|
||||
log.info("7.0 / 3.0 = {}\n", .{seven_div_three});
|
||||
}
|
||||
|
||||
/// ## 测试bool类型
|
||||
/// ## 作者
|
||||
/// - 邵静
|
||||
fn test_bool_variables() void {
|
||||
log.info("{}\n{}\n{}", .{ true and false, true or false, !true });
|
||||
}
|
||||
|
||||
/// ## 测试optional类型
|
||||
/// ## 作者
|
||||
/// - 邵静
|
||||
fn test_optional_variables() void {
|
||||
var optional_value: ?[]const u8 = null;
|
||||
assert(optional_value == null);
|
||||
log.info("optional test one\ntype: {}\nvalue: {?s}\n", .{ @TypeOf(optional_value), optional_value });
|
||||
|
||||
optional_value = "hello";
|
||||
assert(optional_value != null);
|
||||
log.info("optional test two\ntype: {}\nvalue: {?s}\n", .{ @TypeOf(optional_value), optional_value });
|
||||
}
|
||||
|
||||
test "integer variables" {
|
||||
test_integer_variables();
|
||||
}
|
||||
|
||||
test "float variables" {
|
||||
test_float_variables();
|
||||
}
|
||||
|
||||
test "bool variables" {
|
||||
test_bool_variables();
|
||||
}
|
||||
|
||||
test "optional variables" {
|
||||
test_optional_variables();
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
test_integer_variables();
|
||||
test_float_variables();
|
||||
test_bool_variables();
|
||||
test_optional_variables();
|
||||
}
|
53
value/value.md
Normal file
53
value/value.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
# 值
|
||||
|
||||
## 基本数据类型
|
||||
|
||||
| 类型 | C 语言等效类型 | 描述 |
|
||||
| -------------- | ------------------- | -------------------------------------------------------------------------------- |
|
||||
| i8 | int8_t | 有符号的 8 位整数 |
|
||||
| u8 | uint8_t | 无符号的 8 位整数 |
|
||||
| i16 | int16_t | 有符号的 16 位整数 |
|
||||
| u16 | uint16_t | 无符号的 16 位整数 |
|
||||
| i32 | int32_t | 有符号的 32 位整数 |
|
||||
| u32 | uint32_t | 无符号的 32 位整数 |
|
||||
| i64 | int64_t | 有符号的 64 位整数 |
|
||||
| u64 | uint64_t | 无符号的 64 位整数 |
|
||||
| i128 | \_\_int128 | 有符号的 128 位整数 |
|
||||
| u128 | unsigned \_\_int128 | 无符号的 128 位整数 |
|
||||
| isize | intptr_t | 有符号指针大小的整数 |
|
||||
| usize | uintptr_t, size_t | 无符号指针大小的整数 详见[#5185](https://github.com/ziglang/zig/issues/5185) |
|
||||
| c_char | char | 为了与 C 语言 ABI 兼容 |
|
||||
| c_short | short | 为了与 C 语言 ABI 兼容 |
|
||||
| c_ushort | unsigned short | 为了与 C 语言 ABI 兼容 |
|
||||
| c_int | int | 为了与 C 语言 ABI 兼容 |
|
||||
| c_uint | unsigned int | 为了与 C 语言 ABI 兼容 |
|
||||
| c_long | long | 为了与 C 语言 ABI 兼容 |
|
||||
| c_ulong | unsigned long | 为了与 C 语言 ABI 兼容 |
|
||||
| c_longlong | long long | 为了与 C 语言 ABI 兼容 |
|
||||
| c_ulonglong | unsigned long long | 为了与 C 语言 ABI 兼容 |
|
||||
| c_longdouble | long double | 为了与 C 语言 ABI 兼容 |
|
||||
| f16 | \_Float16 | 16 位浮点数(10 位尾数)IEEE-754-2008 binary16 |
|
||||
| f32 | float | 32 位浮点数(23 位尾数)IEEE-754-2008 binary32 |
|
||||
| f64 | double | 64 位浮点数(52 位尾数)IEEE-754-2008 binary64 |
|
||||
| f80 | double | 80 位浮点数(64 位尾数)IEEE-754-2008 80 位扩展精度 |
|
||||
| f128 | \_Float128 | 128 位浮点数(112 位尾数)IEEE-754-2008 binary128 |
|
||||
| bool | bool | 布尔值:真或假 |
|
||||
| anyopaque | void | 用于类型擦除指针 |
|
||||
| void | (none) | 始终为 void{}的值 |
|
||||
| noreturn | (none) | the type of ,,,, and `break` `continue` `return` `unreachable` `while (true)` {} |
|
||||
| type | (none) | 类型的类型 |
|
||||
| anyerror | (none) | 错误代码 |
|
||||
| comptime_int | (none) | 仅限于已知编译时的值。整数字面量的类型。 |
|
||||
| comptime_float | (none) | 仅限于已知编译时的值。浮点字面量的类型。 |
|
||||
|
||||
除了上述的整数类型之外,任意位宽的整数可以通过使用标识符`i`或者`u`后跟数字来引用。例如,标识符 i7 表示有符号的 7 位整数。整数类型的最大允许位宽是 65535。
|
||||
|
||||
## 基本值
|
||||
|
||||
通常用于表示简单的数据或特殊的状态。这些基本值在编程中被广泛使用,通常用于控制逻辑、初始化变量或表示特殊的状态。下面是几种常见的基本值及其描述
|
||||
|
||||
| 名称 | 描述 |
|
||||
| ----------------- | ------------------------------------------------------------------ |
|
||||
| `true` 和 `false` | `bool` 值用于表示逻辑真和逻辑假。 |
|
||||
| `null` | 用于 [`Optioanl`](./optional.md)类型,表示该值为空。 |
|
||||
| `undefined` | 用于表示值未指定或未定义的状态,常用于初始化变量或表示缺少返回值。 |
|
|
@ -8,7 +8,7 @@
|
|||
var variable_name: variable_type = initial_value;
|
||||
```
|
||||
|
||||
例如,声明一个名为`hello`的变量,类型为`[]const u8`,初始值为`""`:
|
||||
例如,声明一个名为`hello`的变量,类型为`[]const u8`,初始值为`""`
|
||||
|
||||
```zig
|
||||
var hello: []const u8 = "";
|
||||
|
@ -28,7 +28,7 @@ std.log.info("{s}\n", .{hello});
|
|||
const variable_name: variable_type = initial_value;
|
||||
```
|
||||
|
||||
例如,声明一个名为`hello`的常量,类型为`[]const u8`,初始值为`""`:
|
||||
例如,声明一个名为`hello`的常量,类型为`[]const u8`,初始值为`""`
|
||||
|
||||
```zig
|
||||
const hello: []const u8 = "Hello, World!";
|
||||
|
|
Loading…
Reference in New Issue
Block a user