将引用的 Vec 转换为值的 Vec 的惯用方法是什么?

编程入门 行业动态 更新时间:2024-10-27 09:39:19
本文介绍了将引用的 Vec 转换为值的 Vec 的惯用方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的函数返回一个 Vec 对元组的引用,但我需要一个 Vec 元组:

My function returns a Vec of references to a tuple, but I need a Vec of tuples:

use std::collections::HashSet; fn main() { let maxs: HashSet<(usize, usize)> = HashSet::new(); let mins: HashSet<(usize, usize)> = HashSet::new(); let intersection = maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>(); }

我应该如何进行转换?

错误:

19 | maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected tuple, found reference | = note: expected type `std::vec::Vec<(usize, usize)>` found type `std::vec::Vec<&(usize, usize)>`

我使用for 循环进行转换,但我不喜欢它,我认为应该有一种模式惯用的方式:

I'm using a for loop to do the conversion, but I don't like it and I think there should be a mode idiomatic way:

for t in maxs.intersection(&mins).collect::<Vec<&(usize, usize)>>().iter() { output.push(**t); }

推荐答案

从 1.36.0 更新

Rust 1.36.0 引入 已复制 的工作原理类似于 cloned,但使用了 Copy 特性,该特性要求副本便宜(例如 memcpy 仅).如果您有基本类型或实现 Copy 的类型,您可以改用它.

Update from 1.36.0

Rust 1.36.0 introduced copied which works like cloned, but uses the Copy trait, which has the requirement, that the copy is cheap (e.g. a memcpy only). If you have primitive types or types that implement Copy you can use that instead.

要使您的示例工作,请使用 克隆然后收集.

To make your example work, use cloned and then collect.

let maxs: HashSet<(usize,usize)> = HashSet::new(); let mins: HashSet<(usize,usize)> = HashSet::new(); let output: Vec<(usize, usize)> = maxs.intersection(&mins).cloned().collect();

此解决方案适用于除实现Clone之外的任何类型:

pub fn clone_vec<T: Clone>(vec: Vec<&T>) -> Vec<T> { vec.into_iter().cloned().collect() }

如果您的函数接受切片,则必须使用 克隆两次.

If your function accepts a slice, you have to use cloned twice.

pub fn clone_slice<T: Clone>(slice: &[&T]) -> Vec<T> { slice.iter().cloned().cloned().collect() }

这样做的原因是 iter() 返回切片引用的迭代器,结果为 &&T.

如果你碰巧有一个类型没有实现 Clone,您可以使用 map

If you happen to have a type that does not implement Clone, you can mimic the behavior with map

pub struct Foo(u32); impl Foo { fn dup(&self) -> Self { Foo(self.0) } } pub fn clone_vec(vec: Vec<&Foo>) -> Vec<Foo> { vec.into_iter().map(|f| f.dup()).collect() } pub fn clone_vec2(vec: Vec<&Foo>) -> Vec<Foo> { // this function is identical to `clone_vec`, but with another syntax vec.into_iter().map(Foo::dup).collect() }

(游乐场)

更多推荐

将引用的 Vec 转换为值的 Vec 的惯用方法是什么?

本文发布于:2023-11-30 10:37:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   方法   Vec

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!