View demo.zig
const assert = @import("std").debug.assert;
/// Compile-time parameters is how Zig implements generics. It is compile-time duck typing and it works mostly the same way that C++ template parameters work. Example:
//fn max(comptime T: type, a: T, b: T) -> T {
// if (a > b) a else b
//}
//fn gimmeTheBiggerFloat(a: f32, b: f32) -> f32 {
// max(f32, a, b)
//}
View istty.rs
pub fn isatty(stream: Stream) -> bool {
let handle = match stream {
Stream::Stdout => winapi::winbase::STD_OUTPUT_HANDLE,
Stream::Stderr => winapi::winbase::STD_ERROR_HANDLE,
};
unsafe {
let handle = kernel32::GetStdHandle(handle);
// check for msys/cygwin
View test.zig
const mem = @import("std").mem;
const assert = @import("std").debug.assert;
const AllocState = struct {
some_mem: [100 * 1024]u8,
index: usize,
fn alloc(a: &mem.Allocator, n: usize) -> %[]u8 {
const self = @ptrCast(&AllocState, a.context);
const result = self.some_mem[self.index ... self.index + n];
View output.txt
[nix-shell:~/dev/zig/build]$ ./zig test ../std/os/path.zig --test-filter resolve
Test 1/1 os.path.resolve...OK
[nix-shell:~/dev/zig/build]$ ./zig test ../std/os/path.zig 
/home/andy/dev/zig/std/buffer.zig:105:24: error: container 'Buffer' has no member called 'initFromBuffer'
    var buf2 = %%Buffer.initFromBuffer(&buf);
                       ^
/home/andy/dev/zig/std/buffer.zig:106:15: error: no member named 'eqlBuffer' in 'Buffer'
    assert(buf.eqlBuffer(&buf2));
              ^
View output.txt
[nix-shell:~/dev/zig/build]$ ./zig build --build-file ../build.zig --help
Usage: ./zig build [steps] [options]
Steps:
default Build the project
test Run all the tests
test-behavior Run the behavior tests
test-std Run the standard library tests
test-compare-output Run the compare output tests
test-build-examples Build the examples
View test.zig
pub fn panic(msg: []const u8) -> noreturn { @breakpoint(); while (true) {} }
comptime {
if (@sizeOf(@typeOf(foo)) == 0) unreachable;
}
// now you can have foo be any function you'd like to play with
fn foo() {
}
// build with:
View hello_att.s
.text
.globl _start
_start:
mov $1, %rax
mov $1, %rdi
mov $msg, %rsi
mov $14, %rdx
syscall
View list.zig
const debug = @import("debug.zig");
const assert = debug.assert;
const mem = @import("mem.zig");
const Allocator = mem.Allocator;
pub fn List(comptime T: type, comptime ct_allocator: ?&Allocator) -> type {
struct {
const Self = this;
const RuntimeAllocatorType = if (ct_allocator == null) &Allocator else void;
View output.ll
; ModuleID = 'test.ll'
source_filename = "./test.zig"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
%MultibootHeader = type <{ i64, i64, i64 }>
!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}