Hey all. Newbie here from a python / go background. I am currently using advent of code as a way to dive into learning zig. I completely understand that the language isn’t 1.0 yet, and that the documentation is all on a best effort at the moment and things change quickly. I also found multiple github issues (that were talking about nicer ways to work with strings) where andrew has explicitly mentioned that a string type will not be coming to zig. Fair enough. So with that in mind, im looking some advice for how someone who has very little experience or knowledge of using pointers or strings as numbers in memory locations, is able to grok how to work with “strings” in zig.
A little context for you (try not to read too much into this code snippet, im not asking for you to solve my AOC problem, im just trying to give context around how my brain works and the current state of my zig knowledge)
const std = @import("std");
pub fn main() !void {
const file = try std.fs.cwd().openFile("file.txt", .{ .mode = .read_only });
defer file.close();
var buffer: [std.mem.page_size]u8 = undefined;
var sum: u32 = 0;
_ = try file.read(&buffer);
var splits = std.mem.split(u8, &buffer, "\n");
while (splits.next()) |line| {
var first: u32 = 0;
var last: u32 = 0;
std.debug.print("{s}", .{line});
for (line) |char| {
if (first == 0) {
first = char;
}
last = char;
std.debug.print("{any}\n", .{@TypeOf(char)});
}
sum += (first + last);
}
std.debug.print("{any}", .{sum});
}
This code outputs the following (for one iteration of the split file)
pppgfivesu8
u8
u8
u8
u8
u8
u8
u8
u8
u8
21815
The intent of the code is to try and read a file full of lines of text which contain numbers, and add up the first and last numbers of each line into an end sum. But again, lets not dwell on the problem too much.
So even though im a complete novice, within an hour i was able to more or less get the above code compiling and running, purely just based on google searching, and reading the very good comments in the zig source (particularly how to use print and formatting, that was very easy to find and understand the in source comments for! likewise for opening a file, it was easy enough to find what the heck the flags are for a file, haha). But, i’ve been stuck on this string problem for 4 days now, because this isn’t intuitive to me at all, and i can’t really find any good sources explaining exactly how to manage strings in zig.
The code output is clearly showing me what im iterating over is NOT a char, its a number (pointer? ascii character number?). and so ofcourse my sum is not going to add up correctly, because every char itself is some number, but also the actual int number in the string, isnt the value i expect (eg 8 == int(8)).
So with all of the above said, its clear that i fundamentally do not understand what strings are in zig, and do not know how to work with them, and how to build a string, or look for a specific character or character representing a number inside of a string, and how to operate on that. What is the best way to go about learning this? Is there any clear learning guides i’ve missed you can point me to? or is this some completely different topic i need to learn first? is this not understanding how types work for example?
any help you can give me would be greatly appreciated!