rust-anthology | Learn Rust | Media library
kandi X-RAY | rust-anthology Summary
kandi X-RAY | rust-anthology Summary
The best short-form writing about Rust, collected. Rust needs more documentation, right? Well, yeah, it does, but there are actually a lot of great Rust docs out there right now, and a lot of great Rust writers! This project aims to collect their work into a single book.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of rust-anthology
rust-anthology Key Features
rust-anthology Examples and Code Snippets
Community Discussions
Trending Discussions on rust-anthology
QUESTION
How is the match
expression implemented at a high level? What happens under the hood for the compiler to know how to direct certain strains of code to one branch vs. the other, figuring it out at compile time? I don't see how this is possible without storing type information for use at runtime.
Something like this example:
...ANSWER
Answered 2022-Mar-04 at 23:16A match
expression does not need runtime type information; as a match
only accepts a single expression of a single known type, by definition it can leverage compile time information.
See also:
match
at compile time vs runtime
At compile time, every match
expression will be verified to be exhaustive: all possible shapes of the value are handled.
At run time, the code will determine which specific match arm is executed. You can think of a match
as implemented via a fancy if
-else
chain.
As we humans tend to be not-extremely-precise when communicating, it's highly likely that some resources blur the line between these two aspects.
Concretely focusing on an enumEnum variants are not standalone types. That is, given an enum Foo
, Foo::Bar
is not a type — it's a value of type Foo
. This is the same as how false
is not a type — it's a value of type bool
. The same logic applies for i32
(type) and 42
(value).
In most cases, enums are implemented as a sea of bytes that correspond to the values each enum variant might be, with each variant's data layered on top of each other. This is known as a union.
Then a discriminant is added next to this soup of bytes. This is an integer value that specifies which variant the value is. Adding the discriminant makes it into a tagged union.
Matching on an enum is conceptually similar to this pseudo-Rust:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rust-anthology
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page