Skip to content

Arc<str> does not have a Default impl #124275

Closed
@Billy-Sheppard

Description

@Billy-Sheppard

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.Libs-SmallLibs issues that are considered "small" or self-containedT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions