使用Drain(..)保留原始结构的内存分配的唯一原因是吗? [英] Is the only reason to use drain(..) to preserve the memory allocation of the original structure?

查看:96
本文介绍了使用Drain(..)保留原始结构的内存分配的唯一原因是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust具有耗尽整个序列的功能,

Rust has a feature to drain an entire sequence,

如果确实需要耗尽整个序列,请使用整个范围..作为参数. -编程Rust

If you do need to drain the entire sequence, use the full range, .., as the argument. - Programming Rust

您为什么需要耗尽整个序列? 我可以看到此文档,但是我看不到任何用例,

Why would you ever need to drain the entire sequence? I can see this documented, but I don't see any use cases for this,

let mut drain = vec.drain(..);

如果流失并没有取得所有权而是清除了原始结构,那么不取得所有权有什么意义呢?我认为可变参考的目的是因为这本书是借来的" ,并且您可以将其还给我.如果清除了原始结构,为什么不拥有" 这本书?您为什么只想借用东西并销毁它?想要 借用向量的子集并清除该子集很有意义-但我似乎无法全神贯注地想借用[em]借用清除原始结构的所有内容.

If draining does not take ownership but clears the original structure, what's the point of not taking ownership? I thought the point of a mutable reference was because the "book was borrowed" and that you could give it back. If the original structure is cleared why not "own" the book? Why would you want to only borrow something and destroy it? It makes sense to want to borrow a subset of a vector, and clearing that subset -- but I can't seem to wrap my head around wanting to borrow the entire thing clearing the original structure.

推荐答案

我认为您是从错误的方向来解决这个问题的.

I think you are approaching this question from the wrong direction.

已经决定要使用RangeBoundsdrain方法,然后需要考虑禁止无限制RangeBounds的利弊.

Having made a decision that you would like to have a drain method that takes a RangeBounds, you then need to consider the pros and cons of disallowing an unbounded RangeBounds.

专业人士

  • 如果您不允许使用无限范围,则尽管使用了drain(..)into_iter(),但不会出现混淆,尽管这两个并不完全相同.
  • If you disallowed an unbounded range, there would be less confusion about whether to use drain(..) vs into_iter(), although noting that these two are not exactly identical.

缺点

  • 实际上,您必须竭尽所能禁止无限范围.

  • You would actually have to go out of your way to disallow an unbounded range.

理想情况下,您希望使用无限制范围来引起编译错误.我是Rust的新手,所以我不确定,但是据我所知没有办法表达drain应该采用实现特征RangeBounds的泛型,只要它不是RangeFull .

Ideally, you would want the use of an unbounded range to cause a compilation error. I'm new to Rust so I am not certain of this, but as far as I know there is no way to express that drain should take a generic that implements trait RangeBounds as long as it is not a RangeFull.

如果在编译时无法检查,那么在运行时您想要什么行为?恐慌似乎是唯一的选择.

If it could not be checked at compile time, what behavior would you want at runtime? A panic would seem to be the only option.

在注释和建议的重复项中均已观察到,完全耗尽后,Vec的长度为0,但容量与调用drain之前的容量相同.通过使用drain允许无限制范围,您可以在某些情况下更轻松地避免重复进行内存分配.

As observed in the comments and in the proposed duplicate, after completely draining it, the Vec will have a length of 0 but a capacity the same as it did before calling drain. By allowing an unbounded range with drain you are making it easier to avoid a repeated memory allocation in some use cases.

至少对我来说,弊大于利.

To me at least, the cons outweigh the pros.

这篇关于使用Drain(..)保留原始结构的内存分配的唯一原因是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆