Skip to content

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

Closed
Valloric opened this issue Mar 3, 2014 · 8 comments
Closed

Matching against a u8 with char literal #12664

Valloric opened this issue Mar 3, 2014 · 8 comments

Comments

@Valloric
Copy link
Contributor

Valloric commented Mar 3, 2014

Boiled down, here's the problem I have:

let f: u8 = '\n' as u8;
let b: u8 = match f { 
  '\n' => 1, 
   _ => 2
};

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 a u8. So something like foo!('\n') would expand to 10.

Either that, or string literals à la C++11: u8'\n'.

@huonw
Copy link
Member

huonw commented Mar 3, 2014

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,
}

@huonw huonw closed this as completed Mar 3, 2014
@sfackler
Copy link
Member

sfackler commented Mar 3, 2014

This also works:

let f: u8 = '\n' as u8;
let b: u8 = match f as char {
    '\n' => 1,
    _ => 2
};

@Valloric
Copy link
Contributor Author

Valloric commented Mar 3, 2014

You can use a static as a (rather verbose) work-around

That's exactly what I'm doing right now. It's really verbose.

@sfackler Matching as char means I'd have to limit the value to being a valid char, which it might not be.

@sfackler
Copy link
Member

sfackler commented Mar 3, 2014

All values from 0 to 255 are valid chars. That's why you can cast from u8 to char, but not from u32 to char.

@Valloric
Copy link
Contributor Author

Valloric commented Mar 3, 2014

@sfackler Hm, I thought UTF-8 made some of those values special...

Still, what I'm actually matching against is a &[u8] with an array pattern and this doesn't help there. But I'll keep it in mind since it might come in handy later, thanks.

@thestinger
Copy link
Contributor

@Valloric: char is a UCS4 code point, and those values are valid but not all encoded as a single byte in UTF-8

@teknopaul
Copy link

Since this thread comes top in Google, rust has byte literals that can be compared C style with ==

if c == b'\n' { 
    ...

@leifmetcalf
Copy link

leifmetcalf commented May 13, 2020

Since this thread comes top in Google, rust has byte literals that can be compared C style with ==

if c == b'\n' { 
    ...

This should be a help message for expected `u8`, found `char`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants