<story><title>Hobby x86 kernel written with Zig</title><url>https://github.com/jzck/kernel-zig</url></story><parent_chain><item><author>guggle</author><text>&amp;gt; I used to do this in Rust but I switched to zig for maintainability and readability over Rust.&lt;p&gt;Can you expand on this ? I&amp;#x27;m asking out of curiosity because I want to learn a &amp;quot;system&amp;quot; programming language (for whatever definition there is to this term). So far I briefly tried Rust and Nim and found the former more difficult to read. I know nothing about Zig, how would you place it between these two ?</text></item><item><author>jackhalford</author><text>Hi, author here! I&amp;#x27;ve just finished writing the pre-emptive multitasking [0](only round robbin though, nothing fancy).&lt;p&gt;I&amp;#x27;m currently writing an ATA driver [1], the idea is to implement ext2.&lt;p&gt;I used to do this in Rust but I switched to zig for maintainability and readability over Rust. It seems that with `comptime` I&amp;#x27;m able to make a lot of things optimal.&lt;p&gt;Overall I have to say kernel programming is _hard_ but very rewarding when it finally works, it&amp;#x27;s really demystifying computers for me!&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;Brendan%27s_Multi-tasking_Tutorial&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;Brendan%27s_Multi-tasking_Tutorial&lt;/a&gt;&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;IDE&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;IDE&lt;/a&gt;</text></item></parent_chain><comment><author>jackhalford</author><text>In general you&amp;#x27;ll find that zig is easier to read than Rust (see the first version of this project in Rust [0]) because it&amp;#x27;s a simpler language. For kernel programming this is even more so the case:&lt;p&gt;* zig has native support for arbitrary sized integers. In Rust I used to do bitshifts, Now I just have a packed struct of u3&amp;#x2F;u5&amp;#x2F;u7 whatever (see `src&amp;#x2F;pci&amp;#x2F;pci.zig`). Of course Rust has a bitflags crate but I didn&amp;#x27;t find it handy, this is a case where native support vs library support means a world of difference.&lt;p&gt;* zig has native support for freestanding target. I used to have to build with Xargo for cross compiling to a custom target, I also was forced to `[nostd]`, and some features I was using forced me to use nightly Rust. In zig I have a simple `build.zig` and a simple `linker.ld` script, just works.&lt;p&gt;* zig has nicer pointer handling. A lot of kernel programming is stuff that Rust considers unsafe anyway. It&amp;#x27;s not uncommon to have to write lines like `unsafe { &lt;i&gt;(ptr as &lt;/i&gt;const u8) }` to deref a pointer in Rust, which is a pain because this kind of thing happens all of the time. Also you have to play around with mut a lot like this: `unsafe { &amp;amp;mut &lt;i&gt;(address as &lt;/i&gt;mut _)`. It just felt wrong a lot of the time, where in zig you have either `const` or `var` and that&amp;#x27;s the end of it.&lt;p&gt;* zig is really fun to write! this is something that comes up often in the community, after years of C it&amp;#x27;s just very refreshing.&lt;p&gt;Some things zig is missing:&lt;p&gt;* Package manager, this is coming soon. [1]&lt;p&gt;* Missing documentation for inline assembly (I think this part is going to get overhauled, as Andrew Kelley is writing an assembler in zig atm [2]).&lt;p&gt;I don&amp;#x27;t know Nim, but I believe it has a garbage collector so it could be tricky to use for kernel programming.&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;jzck&amp;#x2F;kernel-rs&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;jzck&amp;#x2F;kernel-rs&lt;/a&gt;&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;ziglang&amp;#x2F;zig&amp;#x2F;issues&amp;#x2F;943&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;ziglang&amp;#x2F;zig&amp;#x2F;issues&amp;#x2F;943&lt;/a&gt;&lt;p&gt;[2] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.youtube.com&amp;#x2F;watch?v=iWRrkuFCYXQ&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.youtube.com&amp;#x2F;watch?v=iWRrkuFCYXQ&lt;/a&gt;</text></comment>
<story><title>Hobby x86 kernel written with Zig</title><url>https://github.com/jzck/kernel-zig</url></story><parent_chain><item><author>guggle</author><text>&amp;gt; I used to do this in Rust but I switched to zig for maintainability and readability over Rust.&lt;p&gt;Can you expand on this ? I&amp;#x27;m asking out of curiosity because I want to learn a &amp;quot;system&amp;quot; programming language (for whatever definition there is to this term). So far I briefly tried Rust and Nim and found the former more difficult to read. I know nothing about Zig, how would you place it between these two ?</text></item><item><author>jackhalford</author><text>Hi, author here! I&amp;#x27;ve just finished writing the pre-emptive multitasking [0](only round robbin though, nothing fancy).&lt;p&gt;I&amp;#x27;m currently writing an ATA driver [1], the idea is to implement ext2.&lt;p&gt;I used to do this in Rust but I switched to zig for maintainability and readability over Rust. It seems that with `comptime` I&amp;#x27;m able to make a lot of things optimal.&lt;p&gt;Overall I have to say kernel programming is _hard_ but very rewarding when it finally works, it&amp;#x27;s really demystifying computers for me!&lt;p&gt;[0] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;Brendan%27s_Multi-tasking_Tutorial&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;Brendan%27s_Multi-tasking_Tutorial&lt;/a&gt;&lt;p&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;IDE&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;wiki.osdev.org&amp;#x2F;IDE&lt;/a&gt;</text></item></parent_chain><comment><author>simias</author><text>I&amp;#x27;m curious too, I&amp;#x27;m quite familiar with Rust and never written any Zig in my life so I went digging through the source and I find the syntax remarkably similar for the most part. The only thing that stood out is that apparently you can drop the braces for single-line `if` bodies like in C whereas Rust makes them always mandatory but I&amp;#x27;m firmly on Rust&amp;#x27;s side on this one.&lt;p&gt;The part where Rust can get really messy is when you involve generic programing and traits, especially when lifetimes are involved, but I couldn&amp;#x27;t find any obviously generic code in this codebase.&lt;p&gt;EDIT: after digging a bit deeper into Zig it looks like it uses C++&amp;#x27;s style duck typing for metaprograming instead of a trait-based approach like Rust? It definitely removes some overhead to writing generic code but I&amp;#x27;m not sure about readability and maintainability... But I&amp;#x27;m going to stop here because I&amp;#x27;m about to go full fanboy for Rust.</text></comment>