Rust typechecker getting in the way #519
-
I'm new to I'm using v just(b"\r\n").or(just(b'\n')).or(just(b'\r')).or(just(b'\x0C')) And (this time understandably), it complains saying I'm "mixing just(b"\r\n").or(just(b"\n")).or(just(b"\r")).or(just(b"\x0C")) Rust treats all of the parameters as arrays and not slices, and complains they have different lengths...! I also tried the shorter: just(b"\r\n").or(one_of(b"\n\r\x0C")) And also no luck... What can I add to make it infer the right type? Also, is there a way to change implementation to prevent this from happening? Thank you, guys. Edits: just to add context and make things prettier. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I thought I had found it, but it doesn't work... (some other error got in the way and rustic didn't emit the previous error) The error I get is this:
This is the whole function: pub fn newline<'i>() -> impl Parser<'i, &'i [u8], ()> {
just(b"\r\n".as_ref()).or(one_of(b"\r\n\x0C".as_ref())).ignored()
} |
Beta Was this translation helpful? Give feedback.
-
So, you have two options here:
just(b"\r\n").or(just(b"\n")).or(just(b"\r")).or(just(b"\x0C")) Because
just(b"\r\n").ignored()
.or(just(b'\n').ignored())
.or(just(b'\r').ignored())
.or(just(b'\x0C').ignored()) or just(b"\r\n")
.or(just(b'\n').to(b""))
.or(just(b'\r').to(b""))
.or(just(b'\x0C').to(b"")) I'd personally go for option (1) simply because it's shorter, but it's obviously your call. All options will probably compile down to near enough the same code anyway. Side note: I personally find choice((
just(b"\r\n"),
just(b"\n"),
just(b"\r"),
just(b"\x0C"),
)) |
Beta Was this translation helpful? Give feedback.
So, you have two options here:
just
s to accept strings, like so:Because
b"..."
produces an array, this will compile down to exactly the same thing as if you'd just used single characters ([u8; 1]
vsu8
)..map(...)
,.ignored()
,.to(...)
, etc.or
I'd personally go for option (1) simply because it's shorter, but it's obviously your call. All optio…