如何修改集合而不使用它?
我想在返回集合之前就地修改它:
fn main() {
println!("{:?}", compute()); // should print [[2, 1, 0], [5, 4, 3]]
}
// u8 is just a placeholder, so impl Copy is considered cheating :)
fn compute() -> Vec<Vec<u8>> {
let a = vec![0, 1, 2];
let b = vec![3, 4, 5];
let mut result = Vec::new();
result.push(a);
result.push(b);
// avoids allocations from:
//
// result.iter()
// .map(|r| {
// r.reverse()
// r
// })
// .collect::<Vec<_>>()
result.into_iter().for_each(|mut r| r.reverse());
// errors out: the collection was consumed the line above
result
}
已经使用 Vec::new()
分配了一个集合,因此在这里分配第二个集合似乎是一种浪费。我假设这就是 .collect()
所做的。
- 如何避免过度分配?
- 有没有什么简单的方法可以知道发生了多少分配?在 golang 中,它就像
go test -bench=.
一样简单,但在 Rust 中我找不到类似的东西。
I want to modify a collection in place before returning it:
fn main() {
println!("{:?}", compute()); // should print [[2, 1, 0], [5, 4, 3]]
}
// u8 is just a placeholder, so impl Copy is considered cheating :)
fn compute() -> Vec<Vec<u8>> {
let a = vec![0, 1, 2];
let b = vec![3, 4, 5];
let mut result = Vec::new();
result.push(a);
result.push(b);
// avoids allocations from:
//
// result.iter()
// .map(|r| {
// r.reverse()
// r
// })
// .collect::<Vec<_>>()
result.into_iter().for_each(|mut r| r.reverse());
// errors out: the collection was consumed the line above
result
}
A collection was already allocated with Vec::new()
, so allocating a second collection here seems like a waste. I am assuming that's what .collect()
does.
- How do I avoid the allocation in excess?
- Is there any easy way to know how many allocations are happening? In golang it was as easy as
go test -bench=.
, but I can't find anything similar when it comes to Rust.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要对每个内部向量使用
&mut
,因为您可以只使用iter_mut
,它使用&mut Self
代替外部向量的Self
。游乐场
You need to use a
&mut
to each of the inside vectors, for that you can just useiter_mut
which uses&mut Self
instead ofSelf
for the outer vector.Playground