找到你要的答案

Q:Using map with Vectors

Q:利用矢量图

Although vectors are best suited for procedural programming, I would like to use a map function on them. The following snippet works:

fn map<A, B>(u: &Vec<A>, f: &Fn(&A) -> B) -> Vec<B> {
    let mut res: Vec<B> = Vec::with_capacity(u.len());
    for x in u.iter() {
        res.push(f(x));
    }
    res
}

fn f(x: &i32) -> i32 {
    *x + 1
}

fn main() {
    let u = vec![1, 2, 3];
    let v = map(&u, &f);
    println!("{} {} {}", v[0], v[1], v[2]);
}

Why isn't there any such function in the standard library? (and also in std::collections::LinkedList). Is there another way to deal with it?

虽然向量最适合程序编程,我想在它们上使用映射函数。下面的代码片段的作品:

fn map<A, B>(u: &Vec<A>, f: &Fn(&A) -> B) -> Vec<B> {
    let mut res: Vec<B> = Vec::with_capacity(u.len());
    for x in u.iter() {
        res.push(f(x));
    }
    res
}

fn f(x: &i32) -> i32 {
    *x + 1
}

fn main() {
    let u = vec![1, 2, 3];
    let v = map(&u, &f);
    println!("{} {} {}", v[0], v[1], v[2]);
}

为什么标准库中没有这样的函数?(并在std::收藏::链表)。有别的办法处理吗?

answer1: 回答1:

Rust likes to be more general than that; mapping is done over iterators, rather than over solely vectors or slices.

A couple of demonstrations:

let u = vec![1, 2, 3];
let v: Vec<_> = u.iter().map(f).collect();
let u = vec![1, 2, 3];
let v = u.iter().map(|&x| x + 1).collect::<Vec<_>>();

.collect() is probably the most magic part of it, and allows you to collect all the elements of the iterator into a large variety of different types, as shown by the implementors of FromIterator. For example, an iterator of Ts can be collected to Vec<T>, of chars can be collected to a String, of (K, V) pairs to a HashMap<K, V>, and so forth.

This way of working with iterators also means that you often won’t even need to create intermediate vectors where in other languages or with other techniques you would; this is more efficient and typically just as natural.

锈喜欢比这更普遍;映射了迭代器,而不是仅仅载体或片。

一对夫妇的示威游行:

let u = vec![1, 2, 3];
let v: Vec<_> = u.iter().map(f).collect();
let u = vec![1, 2, 3];
let v = u.iter().map(|&x| x + 1).collect::<Vec<_>>();

。collect()可能是它最神奇的部分,你可以收集所有的迭代器的元素为各种不同的类型,如图所示的fromiterator实施者。例如,TS迭代器可以收集到VEC <;T & gt;,字符可以收集到一个字符串,对(k,v)对一个HashMap <;K、V & gt;,等等。

这种工作方式与迭代器也意味着你经常甚至不需要创建中间载体在其他语言或其他技术的你;这是更有效的和通常一样自然。

answer2: 回答2:

As pointed out by bluss, you can also use the mutable iterator to mutate the value in place, without changing the type:

let mut nums = nums;
for num in &mut nums { *num += 1 }
println!("{:p} - {:?}", &nums, nums);

The function Vec::map_in_place was deprecated in Rust 1.3 and is no longer present in Rust 1.4.

Chris Morgan's answer is the best solution 99% of the time. However, there is a specialized function called Vec::map_in_place. This has the benefit of not requiring any additional memory allocations, but it requires that the input and output type are the same size (thanks Levans) and is currently unstable:

fn map_in_place<U, F>(self, f: F) -> Vec<U> 
    where F: FnMut(T) -> U

An example:

#![feature(collections)]

fn main() {
    let nums = vec![1,2,3];
    println!("{:p} - {:?}", &nums, nums);

    let nums = nums.map_in_place(|v| v + 1);
    println!("{:p} - {:?}", &nums, nums);
}

正如所指出的bluss,您还可以使用可变迭代器变异的价值的地方,而不改变类型:

let mut nums = nums;
for num in &mut nums { *num += 1 }
println!("{:p} - {:?}", &nums, nums);

功能:map_in_place VEC:在锈1.3废弃,不再生锈1.4。

克里斯·莫甘的答案是最好的解决方案99%的时间。然而,有一个专门的函数称为血管内皮细胞::map_in_place。这不需要任何额外的内存分配的利益,但它要求输入和输出类型相同的大小(感谢杆菌)是目前不稳定:

fn map_in_place<U, F>(self, f: F) -> Vec<U> 
    where F: FnMut(T) -> U

一个例子:

#![feature(collections)]

fn main() {
    let nums = vec![1,2,3];
    println!("{:p} - {:?}", &nums, nums);

    let nums = nums.map_in_place(|v| v + 1);
    println!("{:p} - {:?}", &nums, nums);
}
rust