Skip to content

Implement const Deref{,Mut} for String#155640

Open
tshakalekholoane wants to merge 1 commit intorust-lang:mainfrom
tshakalekholoane:tshaka/const_convert/string_deref
Open

Implement const Deref{,Mut} for String#155640
tshakalekholoane wants to merge 1 commit intorust-lang:mainfrom
tshakalekholoane:tshaka/const_convert/string_deref

Conversation

@tshakalekholoane
Copy link
Copy Markdown
Contributor

@tshakalekholoane tshakalekholoane commented Apr 22, 2026

Implements const Deref{,Mut} for String.

Tracking issue: #143773.

@rustbot rustbot added 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 Apr 22, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 22, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
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 6 candidates
  • Random selection from Mark-Simulacrum, jhpratt

@tshakalekholoane tshakalekholoane changed the title Implement const Deref{Mut} for String Implement const Deref{,Mut} for String Apr 22, 2026
@tshakalekholoane tshakalekholoane force-pushed the tshaka/const_convert/string_deref branch from 273a00c to ed1e03f Compare April 22, 2026 10:47
@rust-log-analyzer

This comment has been minimized.

@tshakalekholoane tshakalekholoane force-pushed the tshaka/const_convert/string_deref branch from ed1e03f to 40b79f4 Compare April 22, 2026 12:01
@cramertj
Copy link
Copy Markdown
Member

Apologies if there was previously a ruling on this, but waaaayyy back in the day we considered an allocation-free coercion from &'static str to a zero-capacity String. This would've relied upon String not being StableAddress and instead allocating in DerefMut. There were a lot of challenges with this idea, but a lot of value as well.

Providing const DerefMut for String would permanently put a nail in this coffin, so I think there should be a clear "no-go" decision on this idea from lang and libs before stabilizing such an impl.

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 23, 2026

cc @rust-lang/libs ^^

@tshakalekholoane
Copy link
Copy Markdown
Contributor Author

Oh, I was not aware of that. Thank you for the context, @cramertj.

But Vec<T> already has a const DerefMut on track to stabilise, and it faces an identical concern. If that trade-off was accepted for Vec, I would argue it was implicitly accepted for String too, since String is essentially a specialised Vec<u8>.

Is there a distinction between the two that I am missing?

@tgross35
Copy link
Copy Markdown
Contributor

I don't think Vec should be taken as prior art, my guess would be that the same question was just overlooked.

@RalfJung
Copy link
Copy Markdown
Member

FWIW I agree with the points in #155816 that this PR goes too far -- only under truly exceptional circumstances should we do anything with const traits in alloc or std. All the core stuff we need for actual experimentation is in core, after all.

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants