strings weird

main
Jordan Orelli 4 years ago
parent 28ebd12e92
commit c87ae5ce70

2
.gitignore vendored

@ -1 +1,3 @@
zig-cache zig-cache
*.exe
*.pdb

@ -0,0 +1,14 @@
var y: i32 = add(10, x);
const x: i32 = add(12, 34);
test "global variables" {
assert(x == 46);
assert(y == 56);
}
fn add(a: i32, b: i32) i32 {
return a + b;
}
const std = @import("std");
const assert = std.debug.assert;

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
pub fn main() void { pub fn main() !void {
std.debug.print("Hello, {}!\n", .{"World"}); const stdout = std.io.getStdOut().writer();
try stdout.print("Hello, {}!\n", .{"World"});
} }

@ -0,0 +1,9 @@
const std = @import("std");
pub fn main() void {
const msg = "hello this is dog";
var words = std.mem.tokenize(msg, " ");
while (words.next()) |word| {
std.debug.print("{}\n", .{word});
}
}

@ -0,0 +1,12 @@
const std = @import("std");
const assert = std.debug.assert;
test "null @intToPtr" {
// the ? means that the pointer can be null. If you remove it, the test
// fails because you're trying to set a null pointer.
//
// the 0 can also be written 0x0 but I don't know if there's any practical
// difference.
const ptr = @intToPtr(?*i32, 0);
assert(ptr == null);
}

@ -0,0 +1,8 @@
const Point = struct {
x: i32,
y: i32,
pub fn mag() f32 {
return 0;
}
};

@ -0,0 +1,14 @@
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
test "string literals" {
const bytes = "hello";
expect(@TypeOf(bytes) == *const [5:0]u8);
expect(bytes.len == 5);
expect(bytes[1] == 'e');
expect(bytes[5] == 0);
expect('e' == '\x65');
expect('\u{1f4a9}' == 128169);
expect('💯' == 128175);
expect(mem.eql(u8, "hello", "h\x65llo"));
}

@ -0,0 +1,43 @@
const std = @import("std");
const print = std.debug.print;
const os = std.os;
const assert = std.debug.assert;
pub fn main() void {
// integers
const one_plus_one: i32 = 1 + 1;
print("1 + 1 = {}\n", .{one_plus_one});
// floats
const seven_div_three: f32 = 7.0 / 3.0;
print("7.0 / 3.0 = {}\n", .{seven_div_three});
// booleans
print("{}\n{}\n{}\n", .{
true and false,
true or false,
!true,
});
// optional hmm
var optional_value: ?[]const u8 = null;
assert(optional_value == null);
print("\noptional 1\ntype: {}\nvalue: {}\n", .{
@typeName(@TypeOf(optional_value)),
optional_value,
});
// error union
var number_or_error: anyerror!i32 = error.ArgNotFound;
print("\nerror union 1\ntype: {}\nvalue: {}\n", .{
@typeName(@TypeOf(number_or_error)),
number_or_error,
});
number_or_error = 1234;
print("\nerror union2\ntype: {}\nvalue: {}\n", .{
@typeName(@TypeOf(number_or_error)),
number_or_error,
});
}
Loading…
Cancel
Save