-
Notifications
You must be signed in to change notification settings - Fork 563
cfg_select! macro
#2103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
cfg_select! macro
#2103
Conversation
30ce14a to
a93da68
Compare
a93da68 to
11dedff
Compare
11dedff to
6cbeb12
Compare
| r[cfg.cfg_select] | ||
| ### The `cfg_select` macro | ||
|
|
||
| The built-in `cfg_select` macro expands to the right-hand side of the first configuration predicate that evaluates to `true`. | ||
|
|
||
| For example: | ||
|
|
||
| ```rust | ||
| cfg_select! { | ||
| unix => { | ||
| fn foo() { /* unix specific functionality */ } | ||
| } | ||
| target_pointer_width = "32" => { | ||
| fn foo() { /* non-unix, 32-bit functionality */ } | ||
| } | ||
| _ => { | ||
| fn foo() { /* fallback implementation */ } | ||
| } | ||
| } | ||
| ``` | ||
| The `cfg_select` macro can also be used in expression position: | ||
|
|
||
| ```rust | ||
| let is_unix_str = cfg_select! { | ||
| unix => "unix", | ||
| _ => "not unix", | ||
| }; | ||
| ``` | ||
|
|
||
| A `_` can be used to write a configuration predicate that always evaluates to `true`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the Reference PR. It'd be worth filling in the details you provided in rust-lang/rust#149783 (comment), e.g. with the grammar, the removal of one level of braces, the compile error on fallthrough, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can discuss as well how the bodies of each branch must be able to parse.
@rustbot label +S-waiting-on-stabilization