zserge / zserge/zserge.github.io

Fix some typos in code of the "Zig, the small language" post

Open
#8 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
13
Forks
3
PR merge metrics
No merged PRs in 30d

Description

This:

// Single-line comments start with "//", documentation comments start with "///"

const x: i32 = 42; // immutable int32 value that can not be changed
var y: u32 = 5;    // mutable unsigned int32 variable
var z = y; // type can be omitted if it can be inferred from the value

// There is no "int" type, all integers have fixed width.
// Same about floats, there are f16, f32, f64 and f128.
// For indices, "intptr_t" or "size_t" types use "isize" or "usize".

// All function parameters are immutable as if they are passed-by-value.
fn add_two_ints(a: i32, b: i32) i32 {
	if (a == 0) { // if statement looks like C
		return b;
	}
	return a+b;
}

// Arrays have fixed length, here numbers.len == 5
const numbers = [_]i32{ 0, 1, 3, 5, 7 };
// String literals are arrays of []u8
const hello = "hello";
// Arrays can be initialised with repeating values using ** operator
const ten_zero_bytes = [_]u8{0} ** 10;
// Arrays may contain a sentinel value at the end, here array.len == 4 and array[4] == 0.
const array = [_:0]u8 {1, 2, 3, 4};
// Slices are pointers to array data with associated length. The difference between
// arrays and slices is that array's length is known at compile time, while slice
// length is only known at runtime. Like arrays, slices also perform bounds checking.
const full_slice = numbers[0..]; // points at &numbers[0] and has length of 5
const short_slice = numbers[1..3]; // points at &numbers[1] and has length of 2

fn count_nonzero(a: []const i32) i32 {
	var count: i32 = 0;
	for (items) |value| { // "for" works only on arrays and slices, use "while" for generic loops.
		if (value == 0) {
			continue;
		}
		count += 1; // there is no increment operator, but there are shortcuts for +=, *=, >>= etc.
	}
}

pub fn main() void { // main() is a special entry point to your program
	var eight = add_two_ints(3, 5);
	var nonzeros = count_nonzero(full_slice);
}

should be:

// Single-line comments start with "//", documentation comments start with "///"

const x: i32 = 42; // immutable int32 value that can not be changed
var y: u32 = 5;    // mutable unsigned int32 variable
var z = y; // type can be omitted if it can be inferred from the value

// There is no "int" type, all integers have fixed width.
// Same about floats, there are f16, f32, f64 and f128.
// For indices, "intptr_t" or "size_t" types use "isize" or "usize".

// All function parameters are immutable as if they are passed-by-value.
fn add_two_ints(a: i32, b: i32) i32 {
	if (a == 0) { // if statement looks like C
		return b;
	}
	return a+b;
}

// Arrays have fixed length, here numbers.len == 5
const numbers = [_]i32{ 0, 1, 3, 5, 7 };
// String literals are arrays of []u8
const hello = "hello";
// Arrays can be initialised with repeating values using ** operator
const ten_zero_bytes = [_]u8{0} ** 10;
// Arrays may contain a sentinel value at the end, here array.len == 4 and array[4] == 0.
const array = [_:0]u8 {1, 2, 3, 4};
// Slices are pointers to array data with associated length. The difference between
// arrays and slices is that array's length is known at compile time, while slice
// length is only known at runtime. Like arrays, slices also perform bounds checking.
const full_slice = numbers[0..]; // points at &numbers[0] and has length of 5
const short_slice = numbers[1..3]; // points at &numbers[1] and has length of 2

fn count_nonzero(items: []const i32) i32 {
	var count: i32 = 0;
	for (items) |value| { // "for" works only on arrays and slices, use "while" for generic loops.
		if (value == 0) {
			continue;
		}
		count += 1; // there is no increment operator, but there are shortcuts for +=, *=, >>= etc.
	}
	return count;
}

pub fn main() void { // main() is a special entry point to your program
	_ = add_two_ints(3, 5);
	_ = count_nonzero(full_slice);
}

The diff is:

diff -u intro.orig.zig intro.zig 
--- intro.orig.zig	2023-09-22 17:54:21.000000000 +0800
+++ intro.zig	2023-09-22 17:53:12.000000000 +0800
@@ -30,7 +30,7 @@
 const full_slice = numbers[0..]; // points at &numbers[0] and has length of 5
 const short_slice = numbers[1..3]; // points at &numbers[1] and has length of 2
 
-fn count_nonzero(a: []const i32) i32 {
+fn count_nonzero(items: []const i32) i32 {
 	var count: i32 = 0;
 	for (items) |value| { // "for" works only on arrays and slices, use "while" for generic loops.
 		if (value == 0) {
@@ -38,9 +38,10 @@
 		}
 		count += 1; // there is no increment operator, but there are shortcuts for +=, *=, >>= etc.
 	}
+	return count;
 }
 
 pub fn main() void { // main() is a special entry point to your program
-	var eight = add_two_ints(3, 5);
-	var nonzeros = count_nonzero(full_slice);
+	_ = add_two_ints(3, 5);
+	_ = count_nonzero(full_slice);
 }

Tested with Zig 0.11.0 on macOS Monterey 12.6.8 x86_64.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Find the post “Zig, the small language” and compare its embedded Zig example with the proposed diff. Done means the example uses the corrected parameter name and return value, discards the results in main, and verifies successfully with Zig 0.11.0 on the stated macOS system.

Written by the indexing model from the issue text.

Assessment

Tech stack
zig
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.