Add only command to std-rfc/iter#16015
Merged
fdncred merged 4 commits intonushell:mainfrom Jun 23, 2025
Merged
Conversation
Bahex
approved these changes
Jun 20, 2025
fdncred
reviewed
Jun 20, 2025
Co-authored-by: Bahex <Bahex@users.noreply.github.com>
Contributor
|
Thanks. Looks fun! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds the
onlycommand tostd-rfc/iter, which is a command I wrote a while ago that I've found so useful that I think it could have a place in the standard library. It acts similarly toget 0, but ensures that the value actually exists, and there aren't additional values. I find this most useful when chained withwhere, when you want to be certain that no additional elements are accidentally selected when you only mean to get a single element.I'll copy the help page here for additional explanation:
Here's some additional examples showing the errors:

Most of the time I chain this with a simple
where, but here's a couple other real world examples of how I've used this:With
parse, which outputs a table:Ensuring that only one row in a table has a name that ends with a certain suffix:
Unfortunately to get these nice errors I had to collect the stream (and I think the errors are more useful for this). This should be to be mitigated with (something like) #16014.
Putting this in
std/itermight be pushing it, but it seems just close enough that I can't really justify putting it in a different/new module.User-Facing Changes
onlycommand tostd-rfc/iter, which can be used to ensure that a table or list only has a single element.Tests + Formatting
Added a few tests for
onlyincluding error casesAfter Submitting
N/A