3 ms·
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 ! r
by bogomipz 9y ago
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 9y agoIt’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 9y agoI 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 9y agoIt’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 9y agoMy languages are generally Python, Golang, Java. Cheers.
- steveklabnik 9y agoCool :) 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 9y agoThanks for the detailed response and suggested approach, I appreciate it. Working my way through the Nomicon looks like a nice goal. Cheers.
- steveklabnik 9y agoYou’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.
- dmytrish 9y agoThere's a GCC extension that also allows to mark C/C++ functions as `noreturn` (in C, it's a compiler-specific attribute, not a part of the language, unlike Rust): void myexit(int c) __attribute__ ((noreturn));
- earenndil 9y agoActually, `noreturn` is a standard part of c[1]. Can just specify as _Noreturn, or include a header that aliases it directly to noreturn. Don't know about c++, but it probably has it as [[noreturn]]? 1: http://en.cppreference.com/w/c/language/_Noreturn http://en.cppreference.com/w/c/language/_Noreturn