-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Matching against a u8
with char literal
#12664
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
Comments
Dupe of #4334 (specifically #4334 (comment)). You can use a static as a (rather verbose) work-around: static N: u8 = '\n' as u8;
match f {
N => 1,
_ => 2,
} |
This also works: let f: u8 = '\n' as u8;
let b: u8 = match f as char {
'\n' => 1,
_ => 2
}; |
That's exactly what I'm doing right now. It's really verbose. @sfackler Matching |
All values from 0 to 255 are valid |
@sfackler Hm, I thought UTF-8 made some of those values special... Still, what I'm actually matching against is a |
@Valloric: |
Since this thread comes top in Google, rust has byte literals that can be compared C style with ==
|
This should be a help message for |
Boiled down, here's the problem I have:
I'm parsing some binary data and a
u8
value in some cases might be an ASCII char. I'd love to be able to match against these with char literals, but the compiler complains. My actual code is more complicated and involves matching['\\', 'x', c1, c2, ..]
(and similar patterns) against a&[u8]
slice.Possible solutions might involve something like allowing casts in the pattern (
'\n' as u8 => ...
). A more generic solution might be to just have a macro that takes a char literal and returns au8
. So something likefoo!('\n')
would expand to10
.Either that, or string literals à la C++11:
u8'\n'
.The text was updated successfully, but these errors were encountered: