Skip to content

[Feature Request] implement From<array literal> for Vec #67963

Closed
@jyn514

Description

@jyn514

I would like the following code to compile:

fn f<S: Into<Vec<u8>> + Clone>(subscripts: &[S]) -> Vec<Vec<u8>> {
    subscripts.iter().cloned().map(|s| s.into()).collect()
}

fn main() {
    f(&[&b"hi"[..]]);  // this works
    f(&[b"hi"]); // error: `From<&[u8; 2]>` is not satisfied
}

The first call in main is hard to get right, since leaving out any part of the syntax gives confusing errors (without [..], gives From<&&[u8; 2]> is not satisfied, without & gives doesn't have a size known at compile-time).

The second call seems to be the same idea, but doesn't compile since From isn't implemented for array literals.

This can be implemented without trouble on nightly Rust except for the orphan rule: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=21632f9784745afa98663d5e6e471cbb

If someone is willing to mentor I'm happy to turn that playground into a PR :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-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