Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are Rust's boolean and other primitive types implemented?

I was going through the code behind some of the basic types in Rust, e.g. the pleasantly simple implementation of Option<T> or the weird macro magic behind tuple and I was able to find all of the types that I wanted in libcore. All except for one - bool. I couldn't find it anywhere else either.

Where is the code behind bool in Rust? I know this is not the most novel type out there, but I was surprised I could not find it.

Thanks to the answers by Francis and rodrigo, I noticed that the code I found for other primitives was just their traits and related macros, but not actual implementations.

The Rust book states that the primitives are built-in to the language, but I'm not satisfied with this explanation. When were they built in? Can it be traced to the time when the Rust compiler was first built with Rust or did it happen when it was still built in OCaml? Does any relevant code exist?

like image 353
ljedrz Avatar asked Sep 05 '16 18:09

ljedrz


People also ask

How are the Boolean values represented in Rust?

A boolean value in Rust is guaranteed to be 1 or 0 : The bool represents a value, which could only be either true or false . If you cast a bool into an integer, true will be 1 and false will be 0.

What is primitive in Rust?

The Rust language has a number of types that are considered 'primitive'. This means that they're built-in to the language. Rust is structured in such a way that the standard library also provides a number of useful types built on top of these ones, as well, but these are the most primitive.

Why do we need boolean types in rust?

This means that they’re built-in to the language. Rust is structured in such a way that the standard library also provides a number of useful types built on top of these ones, as well, but these are the most primitive. Rust has a built-in boolean type, named bool.

What is a primitive type in rust?

The Rust language has a number of types that are considered ‘primitive’. This means that they’re built-in to the language. Rust is structured in such a way that the standard library also provides a number of useful types built on top of these ones, as well, but these are the most primitive.

Where can I find more documentation for slices in rust?

You can find more documentation for slices in the standard library documentation. Rust’s str type is the most primitive string type. As an unsized type , it’s not very useful by itself, but becomes useful when placed behind a reference, like &str. We'll elaborate further when we cover Strings and references.

What are the different types of rust numbers?

Rust has a variety of numeric types in a few categories: signed and unsigned, fixed and variable, floating-point and integer. These types consist of two parts: the category, and the size. For example, u16 is an unsigned type with sixteen bits of size.


2 Answers

So here's a bit more information about what goes on in the compiler. For starters, as has already been mentioned, the actual operations that occur with booleans are entirely handled by LLVM, and get directly translated to the corresponding CPU instructions. While there are some cases where code just magically appears due to bootstrapping, this is not one of them. The compiler is specifically written to handle these types and emit the correct LLVM instructions.

For the earliest parts of compilation (e.g. during macro expansion) the type bool isn't special. It's just some path with the identifier bool. Eventually around here it will get converted to a primitive type. The actual definition of the type is here.

So now let's look at how the ! operator works. As I mentioned earlier, the code in libcore that does impl Not for bool never gets used. Code in the form !expr gets transformed into <T as Not>::not(expr) here. However, you'll notice that it checks to see if this particular expression is in fact a method call or not, and just leaves it as !expr if it's not meant to be a method call. How does it know? The call in MIR is just a cache lookup. The cache got populated during the type checking pass. Here is where the cache insertion occurs -- basically checking to see if the Not trait is implemented for a given type any time it sees a !. And you'll notice that this line specifically excludes booleans and integral types, which end up compiling down to LLVM instructions directly.

That's the rough picture of how it's defined. You'll find similar code in the same files for other primitives. In theory there could be some line somewhere that was enum bool { true, false } -- but ultimately this same code would still need to override it and emit the appropriate LLVM intrinsics, and integers couldn't be represented this way.

like image 160
sgrif Avatar answered Sep 28 '22 05:09

sgrif


bool is a primitive type. Primitive types and operations on them are implemented by the compiler, i.e. the compiler emits specialized code to perform operations on primitive types.

You'll see that bool implements many traits. Those implementations come from libcore, but they are often implemented by using the corresponding operator. For example, Not::not is implemented by returning !self. For any non-primitive type, this would invoke Not::not recursively and cause a stack overflow, but for primitive types, the compiler resolves the operator differently, and those trait implementations are only provided for the benefit of generic code.

like image 45
Francis Gagné Avatar answered Sep 28 '22 05:09

Francis Gagné