Repository navigation
Expand file tree
/
Copy pathvariables.zig
More file actions
73 lines (56 loc) · 1.8 KB
/
Copy pathvariables.zig
File metadata and controls
73 lines (56 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// standard library
const std = @import("std");
// just get the print library
const print = std.debug.print;
test "test variables" {
const maxIterations : i32 = 10;
var counter : i32 = 0;
while(counter < maxIterations) {
// print decimal
print("iteration {d}\n", .{counter});
counter += 1;
}
// const is imutable
// if you dont use a variable that you declair zig will complain
const number1: u8 = 5;
// zig can infer the type
const const_number = 55;
// so zig doesn't complain about const_nuber not being used
_ = const_number;
// some random name for variable
const @"123 foobar" = 123;
print("random name: {}\n", .{@"123 foobar"});
// if you try to set a const variable zig will complain
// number1 = 5;
// var is mutable
// if you dont update a mutable variable zig will complain
var number2: u8 = 10;
number2 = number2 + number1;
@import("std").debug.print("Number 2: {}\n", .{number2});
var mut_bool = true;
mut_bool = false;
var count: i32 = 10;
count += 1;
// an optional variable
const optional: ?u8 = null;
print("optional: {?}\n", .{optional});
const isStudent: bool = true;
const height: f32 = 5.9;
const letter: u8 = 'A';
const name = "hello zig";
std.debug.print("count: {}\n", .{count});
std.debug.print("height: {}\n", .{height});
std.debug.print("isStudent: {}\n", .{isStudent});
std.debug.print("A: {}\n", .{letter});
std.debug.print("name: {s}\n", .{name});
// custom type
const CustomType = u8;
const test_cust: CustomType = 8;
std.debug.print("CustomType: {}\n", .{test_cust});
}
test "foo" {
print("hello foo", .{});
}
pub fn main() void {
std.debug.print("to run test run `zig test variables.zig\n", .{});
}