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

Add str::reverse method for in place string reversal #353

Closed
alexstanovoy opened this issue Mar 13, 2024 · 4 comments
Closed

Add str::reverse method for in place string reversal #353

alexstanovoy opened this issue Mar 13, 2024 · 4 comments
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@alexstanovoy
Copy link

Proposal

Problem statement

Currently, to reverse a UTF-8 string you need a to write a freestanding function like fn reverse(&mut str) or use s.chars().rev().collect(), which requires an allocation.

Motivating examples or use cases

I haven't remembered a real-world example; it's in my list for a while. Why not though? :)

Solution sketch

The design should be the same as [T]::reverse. First, reverse all bytes, then reverse every char with respect to UTF-8 standard.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@alexstanovoy alexstanovoy added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Mar 13, 2024
@BurntSushi
Copy link
Member

I haven't remembered a real-world example; it's in my list for a while. Why not though? :)

I think we need something more compelling than this for std. And I don't see the correctness problems with reversing based on codepoint acknowledged here.

Note that bstr has reverse_bytes, reverse_chars and reverse_graphemes. Arguably, reverse_graphemes is the most correct and the least likely to produce surprising results. But std doesn't have grapheme segmentation.

@alexstanovoy
Copy link
Author

alexstanovoy commented Mar 13, 2024

But std doesn't have grapheme segmentation.

Just in case, am I right that since std doesn't have grapheme segmentation, the following code is correct for code point reversal? https://pastebin.com/9qnamCrD
If so, I'll add a real example and open a pull request.

@alexstanovoy
Copy link
Author

Yeah, it looks like usually, users expect to reverse graphemes, not the codepoints. Although it may be useful to have idiomatic alternative to .chars().rev().collect() inside the std, after your comment I don't think it's really useful. Thanks for your time! :)

@BurntSushi BurntSushi closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
@scottmcm
Copy link
Member

My favourite example for why .chars().rev() is sketchy: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0cf380056e06601b85f7c6a7887e90ae

[src/main.rs:2:5] "🇸🇪".chars().rev().collect::() = "🇪🇸"

I think the common places you might want this are leetcode style things, where rust-lang/rust#110998 might be the better way, since <[ascii::Char]>::reverse will work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

3 participants