Skip to content

implement Result::is_ok_or and Result::is_err_or - #166741

Open
RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:is_ok_err_or
Open

RalfJung wants to merge 1 commit into
rust-lang:mainfrom
RalfJung:is_ok_err_or

Conversation

@RalfJung

Copy link
Copy Markdown
Member

ACP: rust-lang/libs-team#895
Tracking issue: #166740

@rustbot

rustbot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

@rustbot rustbot added A-option-result Area: Option and Result combinators S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 29, 2026
@rustbot

rustbot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 38 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, joboet

@hanna-kruppe hanna-kruppe left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me, some optional suggestions

I'm not completely sure about the logistics of immediately adopting this in the miri subtree, but happy to rubber-stamp that since it's coming from you and the changes themselves are obviously correct.

View changes since this review

Comment thread library/core/src/result.rs Outdated
Comment on lines +648 to +649
/// let x: Result<String, &str> = Ok("ownership".to_string());
/// assert_eq!(x.as_ref().is_ok_or(|msg| msg.contains("i")), true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's clearer to change the error type to something other than a string and/or the Ok value was a string that doesn't pass the error predicate? Right now, the assertion would still pass if this was typo'd as is_ok_and or x was Err("ownership").

(I realize that the point of this example is to demonstrate how to use this without consuming the Result, but the other aspects of the example also draw attention because they're different from the previous examples.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

Comment thread library/core/src/result.rs Outdated
Comment on lines +739 to +740
/// let x: Result<String, &str> = Ok("ownership".to_string());
/// assert_eq!(x.as_ref().is_err_or(|x| x.len() > 2), true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like above, I'd consider changing the error type or the Ok value so there's no confusing about why the result is true.

... now that I think about it, I would've expected an Err value here for symmetry with the as_ref().is_ok_or example. Maybe it's because I'm looking at both of these methods at the same time, but the spurious difference seems potentially confusing.

Comment thread library/core/src/result.rs Resolved
@RalfJung

Copy link
Copy Markdown
Member Author

I'm not completely sure about the logistics of immediately adopting this in the miri subtree, but happy to rubber-stamp that since it's coming from you and the changes themselves are obviously correct.

Notably, it is only used in tests in Miri, which are never run against the bootstrap sysroot.

@hanna-kruppe

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

📌 Commit dc08970 has been tentatively approved by hanna-kruppe

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors rust-bors Bot 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 Nov 29, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 29, 2026
implement Result::is_ok_or and Result::is_err_or

ACP: rust-lang/libs-team#895
Tracking issue: rust-lang#166740
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 29, 2026
implement Result::is_ok_or and Result::is_err_or

ACP: rust-lang/libs-team#895
Tracking issue: rust-lang#166740
rust-bors Bot pushed a commit that referenced this pull request Nov 30, 2026
Rollup of 2 pull requests

Successful merges:

 - #166741 (implement Result::is_ok_or and Result::is_err_or)
 - #166753 (Avoid missing Debug impl warning for targets without atomics)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-option-result Area: Option and Result combinators 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.

4 participants