Skip to content
Open
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
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const std = @import("std");
const zig_version = @import("builtin").zig_version;

// Used for any unversioned content (for now just /blog)
const current_minor_version = 13;
const current_minor_version = 14;

const version_path = std.fmt.comptimePrint(
"website/versioned_docs/version-0.{}/",
Expand Down
14 changes: 9 additions & 5 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const config = {
maxHits: 7,
disableVersioning: true,
excludeRoutes: [
'/master/**/*', '/0.11/**/*', '/0.12/**/*',
'/master/**/*', '/0.11/**/*', '/0.12/**/*', '/0.13/**/*',
],
}]
],
Expand All @@ -66,19 +66,23 @@ const config = {
editUrl: 'https://github.com/Sobeston/zig.guide/tree/master/website/',
showLastUpdateAuthor: true,
showLastUpdateTime: true,
lastVersion: "0.13",
lastVersion: "0.14",
admonitions: {
keywords: ['cpp', 'go', 'js'],
extendDefaults: true
},
versions: {
"0.14": {
label: 'Zig 0.14.0 (dev)',
"0.15": {
label: 'Zig 0.15.0 (dev)',
path: 'master',
},
"0.14": {
label: 'Zig 0.14.0',
path: '/',
},
"0.13": {
label: 'Zig 0.13.0',
path: '/',
path: '/0.13',
},
"0.12": {
label: 'Zig 0.12.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ toolchain for maintaining **robust**, **optimal**, and **reusable** software.

:::warning

The latest release of Zig is 0.13.0 and is currently unstable.
The latest release of Zig is 0.15.0 and is currently unstable.

:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ toolchain for maintaining **robust**, **optimal**, and **reusable** software.

:::warning

The latest release of Zig is 0.13.0 and is currently unstable.
The latest release of Zig is 0.15.0 and is currently unstable.

:::

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
slug: /
description: Get started with the Zig programming language. Zig is a general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
pagination_prev: null
---

# Welcome

[Zig](https://ziglang.org) is a general-purpose programming language and
toolchain for maintaining **robust**, **optimal**, and **reusable** software.

:::warning

The latest release of Zig is 0.15.0 and is currently unstable.

:::

To follow this guide, we assume you have:

- Prior experience programming
- Some understanding of low-level programming concepts

Knowing a language like C, C++, Rust, Go, Pascal, or similar will help you follow
this guide. You must have an editor, terminal, and internet connection available
to you.
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
---
description: Installation instructions for the Zig programming language on Linux, Windows, and macOS.
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

# Installation

<Tabs
defaultValue="linux"
values={[
{label: 'Linux', value: 'linux'},
{label: 'Windows', value: 'windows'},
{label: 'macOS', value: 'macos'},
]}>
<TabItem value="linux">
Consider getting Zig from your distribution's [package manager](https://github.com/ziglang/zig/wiki/Install-Zig-from-a-Package-Manager). Most major linux distros package the latest Zig release.

### Installing manually

1. [Download](https://ziglang.org/download/#release-0.15.0) a prebuilt version of Zig.

Choose a build of Zig 0.14 for Linux that matches your CPU architecture. If you're unsure which architecture you're using, this can be found with:

```bash
uname -m
```

2. Extract the archive using tar, e.g.

```bash
tar xf zig-linux-x86_64-0.15.0.tar.xz
```

3. Add the location of your Zig binary to your path, e.g.

```bash
echo 'export PATH="$HOME/zig-linux-x86_64-0.15.0:$PATH"' >> ~/.bashrc
```
</TabItem>
<TabItem value="windows">
Consider getting Zig from a package manager such as [chocolatey](https://chocolatey.org/), [scoop](https://scoop.sh/), or [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/#install-winget).

All commands shown are to be used inside Powershell.

```powershell
choco install zig
```
```
winget install zig.zig
```
```
scoop install zig
```

### Installing manually

1. [Download](https://ziglang.org/download/#release-0.14.0) a prebuilt version of Zig.

Choose a build of Zig 0.14 for Windows that matches your CPU architecture. Most Windows systems use `x86_64`, also known as `AMD64`. If you're unsure which architecture you're using, this can be found with:

```powershell
$Env:PROCESSOR_ARCHITECTURE
```

2. Extract Zig.

3. Add Zig to your path:

<Tabs
defaultValue="user"
values={[
{label: 'Current User', value: 'user'},
{label: 'System Wide', value: 'system'},
]}>
<TabItem value="user">

```powershell
[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", "User") + ";C:\_\zig-windows-_",
"User"
)
```
</TabItem>

<TabItem value="system">

```powershell
[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", "Machine") + ";C:\_\zig-windows-_",
"Machine"
)
```
</TabItem>
</Tabs>

Close your terminal and create a new one.


</TabItem>

<TabItem value="macos">
Consider getting Zig from a package manager such as [brew](https://brew.sh/).

```
brew install zig
```
</TabItem>

</Tabs>

### Verifying your install

Verify your installation with `zig version`. The output should look like this:

```
$ zig version
0.14.0
```

### Extras

For completions and go-to-definition in your editor, consider installing the [Zig Language Server](https://github.com/zigtools/zls/#installation).

Consider joining a [Zig community](https://github.com/ziglang/zig/wiki/Community).
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
description: Write your first program using the Zig programming language.
---

# Hello World

Create a file called `main.zig`, with the following contents:

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

Use `zig run main.zig` to build and run it. In this example, `Hello, World!`
will be written to stderr, and is assumed to never fail.

:::warning found 'invalid bytes'
Make sure your `main.zig` file is UTF-8 encoded as the Zig compiler does not currently support other encodings. To re-encode your file as UTF-8, run `zig fmt main.zig` and reopen the file in your editor.
:::
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
pagination_next: language-basics/assignment
---

import CodeBlock from "@theme/CodeBlock";

import Success from "!!raw-loader!./04.running-tests-success.zig";

# Running Tests

In this guide, code examples are often given as runnable tests. Before proceeding, make sure that you can run them successfully.

### Success

Save the following text as `test_pass.zig`, and run `zig test test_pass.zig`; you should read `All 1 tests passed.` in your terminal.

<CodeBlock language="zig">{Success}</CodeBlock>

:::note

Some code examples in this guide will have their imports at the top hidden, make sure to get them by clicking the copy button on the top-right of the code block.

:::

Try the same test without the `try` keyword. What happened?

### Failure

Now, save the following text as `test_fail.zig` and observe it fail.

```zig
const std = @import("std");
const expect = std.testing.expect;
test "always fails" {
try expect(false);
}
```

Which should output something like:

```
Test [1/1] test.always fails... FAIL (TestUnexpectedResult)
/usr/lib/zig/std/testing.zig:515:14: 0x2241ef in expect (test)
if (!ok) return error.TestUnexpectedResult;
^
[...]/test_fail:5:5: 0x224305 in test.always fails (test)
try expect(false);
^
0 passed; 0 skipped; 1 failed.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const std = @import("std");
const expect = std.testing.expect;

test "always succeeds" {
try expect(true);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"label": "Getting Started",
"link": {
"description": "zig.guide - A Guide & Tutorial for the Zig programming language. Install and get started with ziglang here."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
pagination_prev: getting-started/running-tests
---

import CodeBlock from "@theme/CodeBlock";

import Assignment from "!!raw-loader!./01.assignment.zig";
import Undefined from "!!raw-loader!./01.undefined.zig";

# Assignment

Value assignment has the following syntax:
`(const|var) identifier[: type] = value`.

- `const` indicates that `identifier` is a **constant** that stores an immutable
value.
- `var` indicates that `identifier` is a **variable** that stores a mutable
value.
- `: type` is a type annotation for `identifier`, and may be omitted if the data
type of `value` can be inferred.

<CodeBlock language="zig">{Assignment}</CodeBlock>

Constants and variables _must_ have a value. If no known value can be given, the
[`undefined`](https://ziglang.org/documentation/master/#undefined) value, which
coerces to any type, may be used as long as a type annotation is provided.

<CodeBlock language="zig">{Undefined}</CodeBlock>

:::js

If you're familiar with JavaScript, you might be used to using `undefined` as a
way to represent a variable that hasn't been initialised, or to represent an
absense of value.

However in Zig, using `undefined` like this is a bad idea as `undefined` works
very differently. In JavaScript, values can be checked for being undefined,
whereas in Zig, an undefined value is impossible to detect. Usage of undefined
values is not safe. Zig's undefined is "undefined" as in "undefined behaviour",
and should not be used as a stand-in for an optional.

Need optionals? These are [covered later](./23-optionals.mdx).

:::

Where possible, `const` values are preferred over `var` values.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const constant: i32 = 5; // signed 32-bit constant
var variable: u32 = 5000; // unsigned 32-bit variable

// @as performs an explicit type coercion
const inferred_constant = @as(i32, 5);
var inferred_variable = @as(u32, 5000);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a: i32 = undefined;
var b: u32 = undefined;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Arrays

Arrays are denoted by `[N]T`, where `N` is the number of elements in the array
and `T` is the type of those elements (i.e., the array's child type).

For array literals, `N` may be replaced by `_` to infer the size of the array.

```zig
const a = [5]u8{ 'h', 'e', 'l', 'l', 'o' };
const b = [_]u8{ 'w', 'o', 'r', 'l', 'd' };
```

To get the size of an array, simply access the array's `len` field.

```zig
const array = [_]u8{ 'h', 'e', 'l', 'l', 'o' };
const length = array.len; // 5
```
Loading