Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 0 additions & 262 deletions 15_zig_build_fix.txt

This file was deleted.

8 changes: 8 additions & 0 deletions course/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ export default [
text: "版本说明",
collapsed: true,
items: [
{
text: "0.15.1 升级指南",
link: "/update/upgrade-0.15.1",
},
{
text: "0.15.1 版本说明",
link: "/update/0.15.1-description",
},
{
text: "0.14.0 升级指南",
link: "/update/upgrade-0.14.0",
Expand Down
43 changes: 0 additions & 43 deletions course/basic/define-variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,49 +141,6 @@ PS: 说实话,我认为这个设计并不太好。
为什么是作用域顶层呢?实际上,Zig 将一个源码文件看作是一个容器。
:::

## `usingnamespace`

关键字 `usingnamespace` 可以将一个容器中的所有 `pub` 声明混入到当前的容器中。

例如,可以使用 `usingnamespace` 将 `std` 标准库混入到 `main.zig` 这个容器中:

```zig
const T = struct {
usingnamespace @import("std");
};
pub fn main() !void {
T.debug.print("Hello, World!\n", .{});
}
```

注意:无法在结构体 `T` 内部直接使用混入的声明,需要使用 `T.debug` 这种方式才可以!

`usingnamespace` 还可以使用 `pub` 关键字进行修饰,用于转发声明,这常用于组织 API 文件和 C 语言的 `import`。

```zig
pub usingnamespace @cImport({
@cInclude("epoxy/gl.h");
@cInclude("GLFW/glfw3.h");
@cDefine("STBI_ONLY_PNG", "");
@cDefine("STBI_NO_STDIO", "");
@cInclude("stb_image.h");
});
```

相关的使用方法可以是这样的:

```zig
pub usingnamespace @cImport({
@cInclude("xcb/xcb.h");
@cInclude("xcb/xproto.h");
});
```

针对以上引入的头文件,我们可以这样使用 `@This().xcb_generic_event_t`。

> [!IMPORTANT]
> 初次阅读此处感到困惑是正常的。在学习完后续概念后,此处内容将自然理解。

## `threadlocal`

变量可以使用 `threadlocal` 修饰符,使得该变量在不同线程中拥有不同的实例:
Expand Down
5 changes: 3 additions & 2 deletions course/code/15/hello_world.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const std = @import("std");

pub fn main() !void {
try One.main();
try Two.main();
Expand All @@ -8,6 +6,7 @@ pub fn main() !void {

const One = struct {
// #region one
const std = @import("std");
pub fn main() !void {
std.debug.print("Hello, World!\n", .{});
}
Expand All @@ -16,6 +15,7 @@ const One = struct {

const Two = struct {
// #region two
const std = @import("std");
pub fn main() !void {
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
Expand All @@ -35,6 +35,7 @@ const Two = struct {

const Three = struct {
// #region three
const std = @import("std");
pub fn main() !void {
// 定义两个缓冲区
var stdout_buffer: [1024]u8 = undefined; // [!code focus]
Expand Down
8 changes: 6 additions & 2 deletions course/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,13 @@ Zig 本身没有内置的 `@print()` 函数,输出功能通常由标准库的

## 更进一步:线程安全

以上代码在单线程环境下工作良好,但在多线程环境中,多个线程同时调用 `print` 可能会导致输出内容交错混乱。为了保证线程安全,我们需要为 `writer` 添加锁。
以上代码在单线程环境下工作良好,但在多线程环境中,多个线程同时调用 `print` 可能会导致输出内容交错混乱。

你可以使用 `std.Thread.Mutex` 来实现一个线程安全的 `writer`。我们鼓励你阅读[标准库源码](https://ziglang.org/documentation/master/std/#std.Thread.Mutex)来深入了解其工作原理。
为了保证线程安全,我们需要为 `writer` 添加锁。

你可以使用 `std.Thread.Mutex` 来实现一个线程安全的 `writer`。

我们鼓励你阅读[标准库源码](https://ziglang.org/documentation/master/std/#std.Thread.Mutex)来深入了解其工作原理。

## 了解更多

Expand Down
2 changes: 1 addition & 1 deletion course/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ showVersion: false
> 允许函数处理各种数据,以及一小组新的编译器指令,以允许使用反射访问有关这些类型的信息。
> Zig 还旨在提高代码的安全性,它不提供垃圾回收(GC),但是使用可选类型代替 `null` ,这避免了空指针的出现。

![Cover Image](./public/cover_image.png "Cover Image")
![Cover Image](/cover_image.png "Cover Image")

## 为何使用 Zig

Expand Down
2 changes: 1 addition & 1 deletion course/prologue.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ C 很好,非常好,它非常成功,以至于 C 现在已经不再是一门

Zig 的社区需要更多的人来构建,所以我写了这个文档,帮助新人来更好的理解和学习 Zig!

![Cover Image](./public/cover_image.png "Cover Image")
![Cover Image](/cover_image.png "Cover Image")
Loading
Loading