Hi there! I’m grateful that I can ask questions here.
A little about my background
I’m new to Zig, have 6+ months of experience with C, and I started rewriting my latinic-cyrillic transliterator utility in Zig. I’m currently going through resources like ziglings and zig.guide to learn the language. I’ve been advised to directly look at and start reading Zig’s stdlib, and I’m planning to do so when I get a little more comfortable with all the basic syntax.
I’m really new to Zig (5-6 days of knowledge), so pardon me if I’m asking stupid questions :')
The question
I dealt with wchar arrays in C when writing the project as I needed to store cyrillic wide characters such as “Январь” (January). Here, in Zig, looks like it’s much more easier to declare a wide string just with u16 or let the compiler infer it.
But one thing I didn’t understand about this snippet of code:
// these are from external latinizer.zig
pub const cyrillic_abc = [_]u16{ 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я' };
pub const cyrillic_dates = [_][]const u8{ "январ", "феврал", "март", "апрел", "май", "июн", "июл", "август", "сентябр", "октябр", "ноябр", "декабр", "йил" }; // not Russian, but cyrillic Uzbek
// main.zig
const std = @import("std");
const transliterator = @import("transliterator.zig");
pub fn main() !void {
std.debug.print("{u}\n", .{transliterator.latinizer.cyrillic_abc[0]});
std.debug.print("{s}\n", .{transliterator.latinizer.cyrillic_dates[0]});
}
Output:
А
январ
--task finished--
[Process exited 0]
Can someone explain how does a [ ]u8 of [ ]u16 can contain array of wide strings? Or why is it not [_][]const u16, but [_][]const u8 as compiler says? If I’m relying heavily on syntactic sugar, can you please show me how to declare it in maximum verbosity?
Everything works, and I could continue writing the rest of the project, but I really want to understand what’s going on.