
Writing an OS in Rust, Second Edition - aarestad
https://os.phil-opp.com/second-edition/
======
steveklabnik
[https://os.phil-opp.com/news/2018-03-09-pure-rust/](https://os.phil-
opp.com/news/2018-03-09-pure-rust/) provides a lot more context here.

I am _extremely_ excited by this work. One of the hardest parts of hacking on
a hobby OS is the environment. My own project has been hampered for nearly a
year because I switched to Windows and couldn’t get the tooling working. (A
combination of my lack of Windows knowledge and a lack of desire to spend
random Saturdays figuring out obscure toolchain issues.) And that’s with rustc
already being a cross compiler.

This now means that, with Rust, getting going on any platform Rust supports is
now trivial. It’s quite exciting.

~~~
xvilka
Windows in general is not developer-friendly environment. While it provides
great experience for usual user, it seriously lacks the inner beauty of any
*nix system, which is itself a developer environment as a whole.

~~~
jwdunne
I found this. I use a Mac at work but I've got a laptop with Windows on it.

My first instinct was to partition and whack on a Linux distribution. But I
chose to just leave it alone. I don't want to enamour my Mrs with a bootloader
again. Despite the simplicity, the typical UI for a bootloader intimidates
non-techies.

So I tried writing some code on Windows, using PowerShell as a terminal. How
hard could it be? I've done it before. Well, PowerShell is a big leap over
CMD. But it just wasn't unix.

I remember one specific example. I wanted to pipe a list of URLs to a program.
Easy enough in a unix shell. In power shell it's something like

    
    
      Get-Contents <path> | <program>
    

That's a trivial example but when you're doing real work how burdensome does
that base verbosity become?

I know there's this Linux in Windows stuff and Cygwin but really, for real
work, I'll just use a *nix.

~~~
AnIdiotOnTheNet
Powershell makes liberal use of aliasing[0] for when you'rehacking at the
commandline as opposed to writing a script (where the verbosity is actually a
good idea). Also, it has tab complete for cmdlets, variables, and even
switches.

For example, "Get-Content" is aliased to "cat", and "type", which is the old
DOS equivalent of cat, and "gc". And of course you can add your own aliases.
But you're right, it isn't the core-utils you're used to from UNIX, it's
better. So much goddamned better. It aught to be, it was developed with 30+
years of hindsight.

[0][http://ilovepowershell.com/2011/11/03/list-of-top-
powershell...](http://ilovepowershell.com/2011/11/03/list-of-top-powershell-
alias/)

~~~
jwdunne
Point taken. I'll have a look at the list you've linked to and try again.
Thank you for clearing up my ignorance :)

------
bogomipz
What a great book!

I had a question about the section "A Freestanding Rust Binary" specifically
the the "start attribute" section. The author states:

"The ! return type means that the function is diverging, i.e. not allowed to
ever return. This is required because the entry point is not called by any
function, but invoked directly by the operating system or bootloader. So
instead of returning, the entry point should e.g. invoke the exit system call
of the operating system."

Is this explicitly "diverging" marking a Rust specific bit of housekeeping?
For instance I wasn't aware of any parallel in the C runtime but maybe someone
could shed some light on this?

~~~
steveklabnik
It’s part of the Rust typesystem, yes. It’s not the end of the world in this
case if you don’t use it, but it’s the correct thing to do.

! is mostly useful when composing with other things, as it unifies with any
type. return, break, continue, all have the type !. There’s an example of this
being useful in the guessing game chapter of the book, I’d link you but I’m on
mobile.

So in this context, since you never really call this function directly, it’s
return type isn’t super relevant.

~~~
bogomipz
I see, so its "best practices", the code would still compile without that.
Thanks.

I'm curious if you might have any recommendations for a "gentle" introduction
to the Rust type system.

~~~
steveklabnik
It’s not only about the type system, but the book is pretty gentle. It only
lightly covers the more advanced stuff, and leaves a few things out, which are
in the Nomicon.

What languages do you code in normally? I could also give you a short summary.

~~~
bogomipz
My languages are generally Python, Golang, Java. Cheers.

~~~
steveklabnik
Cool :) so, the Java will give you a nice introduction to generics. Rust’s are
a bit stronger, and generally don’t do type erasure. I think given your
background, you should read the generics chapter of the book, and then maybe
poke at error handling, with Option and Result. These will show you how rust
differs from Go; they have almost identical error handling stories, but the
generics plus enums mean they feel very different. From there, traits are
kinda like Java interfaces, but on steroids, and Trait objects do type erasure
like Java does. That’d give you a decent starting place, and you can branch
out from there.

~~~
bogomipz
Thanks for the detailed response and suggested approach, I appreciate it.
Working my way through the Nomicon looks like a nice goal. Cheers.

~~~
steveklabnik
You’re welcome, and good luck! If you run into problems, please don’t hesitate
to jump on IRC or the users forum, we love answering questions.

------
bb88
I've seen a series of these types of articles, and I'm not sure if they're
trying to make the next linux or doing it for fun.

One problem to me is drivers, drivers, drivers. Linux has a bunch of 'em.
Other operating systems, not so much.

~~~
empath75
If you’re targeting xen, you don’t need so many.

~~~
0xcde4c3db
Even targeting hardware, it's not as bad as people make it out to be, provided
you scope platform support and features appropriately. For a lot of hardware
you can get 80% of the utility with 20% of the code, and no new OS jumps out
of the gate with version 1.0 supporting every device on the planet.

------
ape4
That was interesting. I found the headstands necessary to write to a raw
address a bit taxing. In C you'd just use the `volatile` keyword. But being
able to localize unsafe code is a big win.

~~~
kibwen
Worth noting that, as of a week ago in nightly, there are new convenience
methods on unsafe pointers to make using them a bit more ergonomic (though
unsafe operations will still require the `unsafe` keyword, of course). Here's
the document describing the new changes:
[https://github.com/Gankro/rfcs/blob/0048bf68c56dd25cd2afed77...](https://github.com/Gankro/rfcs/blob/0048bf68c56dd25cd2afed7717a3834a22471696/text/0000-unsafe-
pointer-reform.md)

As an example:

    
    
        // This:
        ptr::read(self.ptr.offset(idx as isize))
    
        // Becomes this:
        self.ptr.add(idx).read()
    

This will be in stable Rust on May 10.

