Skip to content

Implement From<&[T; N]> and From<&mut [T; N]> for Vec<T> where T: Clone? #100880

Closed
@EFanZh

Description

@EFanZh
Contributor

Currently we have impl From<&[T]> for Vec<T> where T: Clone and impl From<&mut [T]> for Vec<T> where T: Clone, should From<&[T; N]> and From<&mut [T; N]> also be implemented for Vec<T> where T: Clone?

Activity

vincenzopalazzo

vincenzopalazzo commented on Aug 22, 2022

@vincenzopalazzo
Member

@rustbot claim

vincenzopalazzo

vincenzopalazzo commented on Oct 22, 2022

@vincenzopalazzo
Member

Can you help me with runnable code to understand why the From<&mut [T; N]> [for Vec<T> where T: Clone will be useful and what can be a use case for it?

@rustbot label +T-libs-api

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Oct 22, 2022
vincenzopalazzo

vincenzopalazzo commented on Oct 22, 2022

@vincenzopalazzo
Member

Ah ok, maybe I understand what do you mean, currently we can do

use std::vec::Vec;

#[derive(Clone, Copy, Debug)]
struct Type;

fn main() {
    let mut array = [Type{}];
    let mut vect = vec![];
    vect.extend_from_slice(&array);
    println!("{:?}", vect);  
}

And your proposal is to support

use std::vec::Vec;

#[derive(Clone, Copy, Debug)]
struct Type;

fn main() {
    let mut array = [Type{}];
    let mut vect = Vec::from(&array);
    println!("{:?}", vect);
    
}
EFanZh

EFanZh commented on Oct 24, 2022

@EFanZh
ContributorAuthor

@vincenzopalazzo I don’t have a use case for From<&mut [T; N]>, adding it is just for matching existing From<&mut [T]>. But for From<&[T; N]>, I want to be able to write:

fn foo(value: Option<&[String; 2]>) -> Option<Vec<String>> {
    value.map(Vec::from)
}

instead of

fn foo(value: Option<&[String; 2]>) -> Option<Vec<String>> {
    value.map(<[_; 2]>::as_slice).map(Vec::from)
}
added a commit that references this issue on Sep 28, 2023

Auto merge of rust-lang#111278 - EFanZh:implement-from-array-refs-for…

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

      Participants

      @EFanZh@vincenzopalazzo@rustbot

      Issue actions

        Implement `From<&[T; N]>` and `From<&mut [T; N]>` for `Vec<T>` where `T: Clone`? · Issue #100880 · rust-lang/rust