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

Use split_once in FromStr docs #99335

Merged
merged 1 commit into from
Jul 19, 2022
Merged

Use split_once in FromStr docs #99335

merged 1 commit into from
Jul 19, 2022

Conversation

Dav1dde
Copy link
Contributor

@Dav1dde Dav1dde commented Jul 16, 2022

Current implementation:

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
                                 .split(',')
                                 .collect();

        let x_fromstr = coords[0].parse::<i32>()?;
        let y_fromstr = coords[1].parse::<i32>()?;

        Ok(Point { x: x_fromstr, y: y_fromstr })
    }

Creating the vector is not necessary, split_once does the job better.

Alternatively we could also remove trim_matches with strip_prefix and strip_suffix:

        let (x, y) = s
            .strip_prefix('(')
            .and_then(|s| s.strip_suffix(')'))
            .and_then(|s| s.split_once(','))
            .unwrap();

The question is how much 'correctness' is too much and distracts from the example. In a real implementation you would also not unwrap (or originally access the vector without bounds checks), but implementing a custom Error and adding a From<ParseIntError> and implementing the Error trait adds a lot of code to the example which is not relevant to the FromStr trait.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 16, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @kennytm (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot
Copy link
Collaborator

rustbot commented Jul 16, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 16, 2022
@JohnTitor
Copy link
Member

Removing unwrap is indeed overkill but I'd like to have strip_{prefix, suffix}, could you update the example?

@JohnTitor JohnTitor assigned JohnTitor and unassigned kennytm Jul 18, 2022
@JohnTitor JohnTitor added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2022
@Dav1dde
Copy link
Contributor Author

Dav1dde commented Jul 18, 2022

I've updated the example to use strip_prefix and strip_suffix (like mentioned in the original PR).

@JohnTitor I am not sure what the proper process here is, can I change the label back to S-waiting-on-review? I couldn't find anything in the docs.

@Dav1dde
Copy link
Contributor Author

Dav1dde commented Jul 18, 2022

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 18, 2022
@JohnTitor
Copy link
Member

I am not sure what the proper process here is, can I change the label back to S-waiting-on-review? I couldn't find anything in the docs.

Yeah, you're right, you could also use the shorthand described in https://github.com/rust-lang/triagebot/wiki/Shortcuts.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 18, 2022

📌 Commit c1c1abc has been approved by JohnTitor

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 18, 2022
Use split_once in FromStr docs

Current implementation:

```rust
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let coords: Vec<&str> = s.trim_matches(|p| p == '(' || p == ')' )
                                 .split(',')
                                 .collect();

        let x_fromstr = coords[0].parse::<i32>()?;
        let y_fromstr = coords[1].parse::<i32>()?;

        Ok(Point { x: x_fromstr, y: y_fromstr })
    }
```

Creating the vector is not necessary, `split_once` does the job better.

Alternatively we could also remove `trim_matches` with `strip_prefix` and `strip_suffix`:

```rust
        let (x, y) = s
            .strip_prefix('(')
            .and_then(|s| s.strip_suffix(')'))
            .and_then(|s| s.split_once(','))
            .unwrap();
```

The question is how much 'correctness' is too much and distracts from the example. In a real implementation you would also not unwrap (or originally access the vector without bounds checks), but implementing a custom Error and adding a `From<ParseIntError>` and implementing the `Error` trait adds a lot of code to the example which is not relevant to the `FromStr` trait.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 19, 2022
Rollup of 8 pull requests

Successful merges:

 - rust-lang#97183 (wf-check generators)
 - rust-lang#98320 (Mention first and last macro in backtrace)
 - rust-lang#99335 (Use split_once in FromStr docs)
 - rust-lang#99347 (Use `LocalDefId` in `OpaqueTypeKey`)
 - rust-lang#99392 (Fix debuginfo tests.)
 - rust-lang#99404 (Use span_bug for unexpected field projection type)
 - rust-lang#99410 (Update invalid atomic ordering lint)
 - rust-lang#99434 (Fix `Skip::next` for non-fused inner iterators)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 9f6a2fd into rust-lang:master Jul 19, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants