Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
fix duplicated
Browse files Browse the repository at this point in the history
  • Loading branch information
jiacai2050 committed Jan 3, 2024
1 parent c352730 commit 4d63b45
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

with:
fetch-depth: 0
- name: Setup mdBook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: "latest"

- name: Cache Cargo
uses: actions/cache@v3
with:
path: |
~/.cargo/bin
- name: Setup Pages
id: pages
uses: actions/configure-pages@v4

- run: mdbook build
- run: |
cargo install mdbook-last-changed
mdbook build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
Expand Down
10 changes: 4 additions & 6 deletions 04-style-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn add(a: i64, b: i64) i64 {

除了使用`_ = b`之外,我们还可以直接用`_`来命名函数`add`的形参。但是,在我看来,这样做会牺牲代码的可读性,读者会猜测,这个未使用的形参到底是什么:

```
```zig
fn add(a: i64, _: i64) i64 {
```

Expand Down Expand Up @@ -76,17 +76,15 @@ Zig 代码采用 4 个空格进行缩进。我个人会因为客观上更方便

Zig 的函数名采用了驼峰命名法(camelCase),而变量名会采用小写加下划线(snake case)的命名方式。类型则采用的是 PascalCase 风格。除了这三条规则外,一个有趣的交叉规则是,如果一个变量表示一个类型,或者一个函数返回一个类型,那么这个变量或者函数遵循 PascalCase。在之前的章节中,其实已经见到了这个例子,不过,可能你没有注意到:

```
```zig
std.debug.print("{any}\n", .{@TypeOf(.{.year = 2023, .month = 8})});
```

我们已经看到过一些内置函数:`@import``@rem``@intCast`。因为这些都是函数,他们的命名遵循驼峰命名法。`@TypeOf`也是一个内置函数,但是他遵循 PascalCase,为何?因为他返回的是一个类型,因此它的命名采用了类型命名方法。当我们使用一个变量,去接收`@TypeOf`的返回值,这个变量也需要遵循类型命名规则(即 PascalCase):

```
```zig
const T = @TypeOf(3);
std.debug.print("{any}\n", .{T});
```

可执行文件 `zig` 包含有一个 `fmt` 命令,在给定一个文件或目录时,它将根据 Zig 自己的样式指南格式化该文件。但是它并不涵盖所有规则,例如,它会调整缩进与花括号位置,但不会更改标识符大小写。

`zig`命令包含一个`fmt` 子命令,可以用来,基于 zig 的风格规范,格式化一个文件或者目录下的所有文件。`zig fmt`并没有包含所有上述的规则,比如它能够调整缩排的空格,以及大括号`{`的位置,但是它不会调整标识符的大小写。
`zig` 命令包含一个 `fmt` 子命令,在给定一个文件或目录时,它会根据 Zig 的编码风格对文件进行格式化。但它并没有包含所有上述的规则,比如它能够调整缩排,以及花括号`{`的位置,但是它不会调整标识符的大小写。
4 changes: 2 additions & 2 deletions 06-stack-memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

在上一部分中,我们可视化了 `main``levelUp` 函数的内存,每个函数都有一个 User:

```bash
```text
main: user -> ------------- (id: 1043368d0)
| 1 |
------------- (power: 1043368d8)
Expand Down Expand Up @@ -53,7 +53,7 @@ levelUp: user -> ------------- (id: 1043368ec) |

`levelUp` 紧接在 `main` 之后是有原因的:这是我们的简化版调用栈。当我们的程序启动时,`main` 及其局部变量被推入调用栈。当 `levelUp` 被调用时,它的参数和任何局部变量都会被添加到调用栈上。重要的是,当 `levelUp` 返回时,它会从栈中弹出。 在 `levelUp` 返回并且控制权回到 `main` 后,我们的调用栈如下所示:

```bash
```text
main: user -> ------------- (id: 1043368d0)
| 1 |
------------- (power: 1043368d8)
Expand Down
3 changes: 2 additions & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ src = "."

[output.html]
git-repository-url = "https://github.com/zigcc/learning-zig"
git-repository-icon = "fa-github"
edit-url-template = "https://github.com/zigcc/learning-zig/edit/main/{path}"
additional-js = ["static/zig-hl.js"]
additional-css = ["static/last-changed.css"]
site-url = "/learning-zig/"

[output.html.print]
Expand Down
6 changes: 6 additions & 0 deletions static/last-changed.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
footer {
font-size: 0.8em;
text-align: center;
border-top: 1px solid black;
padding: 5px 0;
}
73 changes: 73 additions & 0 deletions static/zig-hl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const zigLanguageSupport = (hljs) => {
return {
name: "Zig",
aliases: ["zig"],
keywords: {
keyword:
"unreachable continue errdefer suspend return resume cancel break catch async await defer asm try " +
"threadlocal linksection allowzero stdcallcc volatile comptime noalias nakedcc inline export packed extern align const pub var " +
"struct union error enum while for switch orelse else and if or usingnamespace test fn",
type: "comptime_float comptime_int c_longdouble c_ulonglong c_longlong c_voidi8 noreturn c_ushort anyerror promise c_short c_ulong c_uint c_long isize c_int usize void f128 i128 type bool u128 u16 f64 f32 u64 i16 f16 i32 u32 i64 u8 i0 u0",
literal: "undefined false true null",
},
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.QUOTE_STRING_MODE,
hljs.APOS_STRING_MODE,
hljs.C_NUMBER_MODE,
{
className: "string",
begin: "@[a-zA-Z_]\\w*",
},
{
className: "meta",
begin: /@[a-zA-Z_]\w*/,
},
{
className: "symbol",
begin: /'[a-zA-Z_][a-zA-Z0-9_]*'/,
},
{
className: "literal",
begin: /\\[xuU][a-fA-F0-9]+/,
},
{
className: "number",
begin: /\b0x[0-9a-fA-F]+/,
},
{
className: "number",
begin: /\b0b[01]+/,
},
{
className: "number",
begin: /\b0o[0-7]+/,
},
{
className: "number",
begin: /\b[0-9]+\b/,
},
hljs.REGEXP_MODE,
{
// multiline string literals
className: "string",
begin: /\\/, end: /$/,
relevance: 0,
contains: [
{
begin: /\\/, end: /$/,
relevance: 0,
},
],
},
],
};
};

document.addEventListener('DOMContentLoaded', (event) => {
if (typeof hljs !== 'undefined') {
console.log("register zig support");
hljs.registerLanguage("zig", zigLanguageSupport);
hljs.initHighlighting();
}
});

0 comments on commit 4d63b45

Please sign in to comment.