-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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.as_str() for easy Deref to string slices #129550
Conversation
r? libs-api |
We discussed this in the @rust-lang/libs-api meeting. We agree that such a method is useful, however it would be better to place this method on |
@rustbot author |
I've created a tracking issue: #130366 |
r=me once this passes CI. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@bors r=joshtriplett,dtolnay |
…,dtolnay Add str.as_str() for easy Deref to string slices Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected. Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s. `Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection. `String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#122565 (Try to write the panic message with a single `write_all` call) - rust-lang#129550 (Add str.as_str() for easy Deref to string slices) - rust-lang#130659 (Support `char::encode_utf16` in const scenarios.) - rust-lang#130705 (No longer mark RTN as incomplete) - rust-lang#130712 (Don't call `ty::Const::normalize` in error reporting) - rust-lang#130713 (Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const.) - rust-lang#130714 (Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`) - rust-lang#130715 (Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis) r? `@ghost` `@rustbot` modify labels: rollup
…,dtolnay Add str.as_str() for easy Deref to string slices Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected. Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s. `Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection. `String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#129550 (Add str.as_str() for easy Deref to string slices) - rust-lang#130344 (Handle unsized consts with type `str` in v0 symbol mangling) - rust-lang#130659 (Support `char::encode_utf16` in const scenarios.) - rust-lang#130705 (No longer mark RTN as incomplete) - rust-lang#130712 (Don't call `ty::Const::normalize` in error reporting) - rust-lang#130713 (Mark `u8::make_ascii_uppercase` and `u8::make_ascii_lowercase` as const.) - rust-lang#130714 (Introduce `structurally_normalize_const`, use it in `rustc_hir_typeck`) - rust-lang#130715 (Replace calls to `ty::Const::{try_}eval` in mir build/pattern analysis) - rust-lang#130723 (Add test for `available_parallelism()`) - rust-lang#130726 (tests: Remove spuriously failing vec-tryinto-array codegen test) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#129550 - kornelski:boxasstr, r=joshtriplett,dtolnay Add str.as_str() for easy Deref to string slices Working with `Box<str>` is cumbersome, because in places like `iter.filter()` it can end up being `&Box<str>` or even `&&Box<str>`, and such type doesn't always get auto-dereferenced as expected. Dereferencing such box to `&str` requires ugly syntax like `&**boxed_str` or `&***boxed_str`, with the exact amount of `*`s. `Box<str>` is [not easily comparable with other string types](rust-lang#129852) via `PartialEq`. `Box<str>` won't work for lookups in types like `HashSet<String>`, because `Borrow<String>` won't take types like `&Box<str>`. OTOH `set.contains(s.as_str())` works nicely regardless of levels of indirection. `String` has a simple solution for this: the `as_str()` method, and `Box<str>` should too.
Working with
Box<str>
is cumbersome, because in places likeiter.filter()
it can end up being&Box<str>
or even&&Box<str>
, and such type doesn't always get auto-dereferenced as expected.Dereferencing such box to
&str
requires ugly syntax like&**boxed_str
or&***boxed_str
, with the exact amount of*
s.Box<str>
is not easily comparable with other string types viaPartialEq
.Box<str>
won't work for lookups in types likeHashSet<String>
, becauseBorrow<String>
won't take types like&Box<str>
. OTOHset.contains(s.as_str())
works nicely regardless of levels of indirection.String
has a simple solution for this: theas_str()
method, andBox<str>
should too.