9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RustでVecのCloneをする

Last updated at Posted at 2022-03-02

環境

  • rust 1.58.1

やりたかったこと

vecのCloneをしたかった。

答え

let v = vec!["1".to_string(),"2".to_string(),"3".to_string()]; // こんなvecがあったとして
let v2 = v.to_vec(); // これでできる

// 自前structの場合もCloneをつけておけば
#[derive(Debug, Clone)]
struct S1(Result<String, String>);;

let ss1 = v.into_iter()
        .map(|x| S1(Ok(x.to_string())))
        .collect::<Vec<S1>>();
let ss2 = ss1.to_vec(); // こんな感じでCloneできます

備考

  • いろんな方法があるかとは思うが、これが一番簡単そうだった
  • 探し当てるのに少し時間かかったのでメモ
9
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?