Closed
Description
I tried this code:
let my_var: Arc<str> = Default::default();
assert_eq!(my_var, Arc::from(""));
I expected to see this happen:
Compiles and Runs successfully.
Instead, this happened:
error[E0277]: the trait bound `str: Default` is not satisfied
--> src/main.rs:4:28
|
4 | let my_var: Arc<str> = Default::default();
| ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `str`, which is required by `Arc<str>: Default`
|
= help: the following other types implement trait `Default`:
&str
&mut str
= note: required for `Arc<str>` to implement `Default`
For more information about this error, try `rustc --explain E0277`.
It does work for Arc<&str>
but not for Arc<str>
. This is because str
doesn't have a Default impl (presumably due to being unsized).
In theory its as "simple" as adding this into the std/alloc
library, which does pass ./x.py check library/core
on my machine. However this is definitely not the most efficient implementation of the intended result, and perhaps an implementation similar to impl Default for Box<str>
would suffice more acutely.
#[cfg(not(no_global_oom_handling))]
impl Default for Arc<str> {
/// Creates an empty str inside an Arc
#[inline]
fn default() -> Self {
Arc::from("")
}
}
Meta
rustc --version --verbose
: Stable channel: Build using the Stable version: 1.77.2
.
Applies on nightly: Nightly channel: Build using the Nightly version: 1.79.0-nightly (2024-04-21 fb898629a26e4acec59c)
also.