-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzig.zig
More file actions
51 lines (40 loc) · 1.16 KB
/
zig.zig
File metadata and controls
51 lines (40 loc) · 1.16 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
// Comment
const std = @import("std");
const Point = struct {
x: f32,
y: f32,
pub fn distance(self: Point, other: Point) f32 {
return std.math.sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2);
}
};
fn greet(name: []const u8) void {
std.debug.print("Hello, {}!\n", .{name});
}
pub fn main() void {
var stdout = std.io.getStdOut().writer();
const point1 = Point{ .x = 1.0, .y = 2.0 };
const point2 = Point{ .x = 4.0, .y = 6.0 };
const distance = point1.distance(point2);
_ = stdout.print("Distance: {}\n", .{distance});
const names = [_][]const u8{"Alice", "Bob", "Charlie"};
for (names) |name| {
greet(name);
}
const multi_line_string = \\ Hello
\\ World
;
var counter: u32 = 0;
while (counter < 10) : (counter += 1) {
if (counter % 2 == 0) {
std.debug.print("{} is even\n", .{counter});
} else {
std.debug.print("{} is odd\n", .{counter});
}
}
// Error handling
const result = try someFunction();
_ = stdout.print("Result: {}\n", .{result});
}
fn someFunction() !i32 {
return 42;
}