Skip to content
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

"TRUE" does not parse into boolean #101870

Closed
djensen1997 opened this issue Sep 15, 2022 · 3 comments · Fixed by #102094
Closed

"TRUE" does not parse into boolean #101870

djensen1997 opened this issue Sep 15, 2022 · 3 comments · Fixed by #102094
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@djensen1997
Copy link

Summary of the issue

When taking input into my program, I ran into a situation where the user would pass "TRUE" into a field that I was attempting to use the .parse() method into a boolean:

fn input(value: &str) -> Result<(), MyErrorType> {
  let new_val: bool = value.parse().map_err(|_| MyErrorType{})?;
  Ok(())
}

when I split this specific line of code into a fresh rust project:

fn main() {
    let x: bool = "TRUE".parse().expect("NOPE");
}

I got the output:
thread 'main' panicked at 'NOPE: ParseBoolError', src/main.rs:20:34

What I expect to happen

I would expect rust to see the string value "TRUE" and read that as a boolean with the value true

What actually happens

I am getting an error return with MyErrorType

@CollinEMac
Copy link

CollinEMac commented Sep 15, 2022

It looks like Rust properly parses "true" and "false" in lowercase to a boolean but not the capitalized "TRUE" or "FALSE".

This is documented here:
https://doc.rust-lang.org/std/primitive.bool.html#method.from_str

@Noratrieb
Copy link
Member

It's not clearly documented what exactly is and is not supposed to be parsed, but I think that the current behaviour of only parsing true and false is the best solution here, and changing the behaviour of functions like this is generally a breaking change.

For comparison, here's what a few other languages do:

@GuillaumeGomez
Copy link
Member

I opened #102094 which should fix this issue.

@dtolnay dtolnay added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Sep 23, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 23, 2022
…ng-docs, r=scottmcm

Add missing documentation for `bool::from_str`

Fixes rust-lang#101870.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Sep 23, 2022
…ng-docs, r=scottmcm

Add missing documentation for `bool::from_str`

Fixes rust-lang#101870.
@bors bors closed this as completed in 986fc4b Sep 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants