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

FromStr is not implemented for char #24939

Closed
oli-obk opened this issue Apr 29, 2015 · 7 comments · Fixed by #42271
Closed

FromStr is not implemented for char #24939

oli-obk opened this issue Apr 29, 2015 · 7 comments · Fixed by #42271
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Apr 29, 2015

I'm not sure if this is an oversight. But the implementation should be trivial. I'll add tests and implement it, if it is fine.

enum ParseCharError { EmptyString, TooManyChars }
impl FromStr for char {
    type Err = ParseCharError;
    #[inline]
    fn from_str(s: &str) -> Result<char, ParseCharError> {
        match s.len() {
            0 => Err(ParseCharError::EmptyString),
            1 => Ok(s[0]),
            _ => Err(ParseCharError::TooManyChars),
        }
    }
}
@bluss
Copy link
Member

bluss commented Apr 29, 2015

Strings aren't indexable and .len() doesn't count the number of chars. 😄

What comes to mind first is an iterator inspecting the two first chars:

let mut chars = s.chars();
match (chars.next(), chars.next()) {
    (None, None) => // empty
    (Some(c), None) => Ok(c),
    _  => // Too many
}

@oli-obk
Copy link
Contributor Author

oli-obk commented Apr 29, 2015

xD I didn't test my code, but you get the gist.
I had a chars() impl at first, but then edited it

@steveklabnik
Copy link
Member

cc @aturon

@steveklabnik
Copy link
Member

Triage: this has not been added.

fn main() {
    let c = String::from("c");
    let c: char = c.parse().unwrap();
}

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@aturon
Copy link
Member

aturon commented May 7, 2017

PRs welcome!

@tinaun
Copy link
Contributor

tinaun commented May 27, 2017

I can take this.

I was also thinking of adding a FromStr impl for &'a str, but unsure if it would be better to wait for never_type to get closer to stabilization and use that instead of adding another empty enum to std

@tinaun
Copy link
Contributor

tinaun commented May 27, 2017

wait, that wouldn't work bc of lifetime issues regardless, right? :(

frewsxcv added a commit to frewsxcv/rust that referenced this issue Jun 20, 2017
add `FromStr` Impl for `char`

fixes rust-lang#24939.

is it possible to use pub(restricted) instead of using a stability attribute for the internal error representation? is it needed at all?
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