简体   繁体   English

结构中向量内字符串切片的 Rust 生命周期

[英]Rust lifetime of string slices within a vector within a struct

While writing a program of mine, I came across a structure containing a vector of strings.在编写我的程序时,我遇到了一个包含字符串向量的结构。 My (basic) knowledge of Rust led me to wonder whether a Vec<&str> within a structure like this:我对 Rust 的(基本)知识让我想知道Vec<&str>是否在这样的结构中:

struct StrSliceVector<'a> {
    v: Vec<&'a str>
}

(that has the lifetime strongly related to the structure) is more efficient or allocates less heap-memory than this one: (与结构密切相关的生命周期)比这个更有效或分配更少的堆内存:

struct StringVector {
    v: Vec<String>
}

I imagine that since in StringVector the String s are allocated in the heap, then it would result in a vector (also in the heap) containing pointers to other memory locations with the actual strings.我想,由于在StringVector中, String是在堆中分配的,因此它会产生一个向量(也在堆中),其中包含指向具有实际字符串的其他内存位置的指针。 Does StrSliceVector allocate the strings internally to the vector heap-space? StrSliceVector是否在内部将字符串分配给向量堆空间?

When you create the Vec<String> , you must decide whether you wish to move existing String s into the vector, or clone them in. If you move them in then no additional heap allocations are required, so it's the same total heap allocations as Vec<&'a str> from this point of view.创建Vec<String>时,您必须决定是要将现有String移入向量中,还是将它们clone进去。如果将它们移入,则不需要额外的堆分配,因此总堆分配与Vec<&'a str>从这个角度来看。 Which is more appropriate depends on your use case.哪个更合适取决于您的用例。 What do you wish to do with the elements of the vector?你想对向量的元素做什么?

If you don't require ownership of the strings in the vector for the operations you intend to perform, the string slice version should be your first instinct as it means the strings are still available for manipulation elsewhere in the code, after the vector has been dropped.如果您不要求对要执行的操作拥有向量中的字符串的所有权,则字符串切片版本应该是您的第一直觉,因为这意味着字符串仍然可用于在代码中的其他地方进行操作,在向量已被掉了。

A &str takes up two usize s of memory (not including the string data its referencing) while a String takes up three usize s. &str占用两个usize内存(不包括其引用的字符串数据),而String占用三个usize But if this matters to you, then you can replace String with Box<str> to get the same size as a &str just as an owned version.但是,如果这对您很重要,那么您可以将String替换为Box<str>以获得与&str相同的大小,就像拥有的版本一样。 Then you're back at simply looking at the logical difference of "owned" vs "borrowed".然后你回到简单地查看“拥有”与“借用”的逻辑差异。

A &str only references existing data, while the String owns its data. &str仅引用现有数据,而String拥有其数据。 The only reason that the String version would take significantly more memory than &str would be if the strings are already owned elsewhere: in another structure or from a string literal, for example. String版本比&str占用更多内存的唯一原因是如果字符串已经在其他地方拥有:例如,在另一个结构中或来自字符串文字。 If they are, then you're just copying what already exists, therefore using more memory.如果是,那么您只是在复制已经存在的内容,因此会使用更多内存。 If you want a datatype that can handle both owned and borrowed data to save on allocations, consider using a Cow .如果您想要一个可以处理拥有和借用数据的数据类型以节省分配,请考虑使用Cow

Does StrSliceVector allocate the strings internally to the vector heap-space? StrSliceVector是否在内部将字符串分配给向量堆空间?

Both are &str and String are similar that in-place they are basically just pointers;两者都是&strString相似,就地它们基本上只是指针; the data will always exist elsewhere and not in-line with the Vec elements.数据将始终存在于其他地方,而不是与Vec元素一致。

So in broad terms, &str will be more space-efficient, but lifetimes introduce more constraints that may make StrSliceVector<'a> unviable depending on how you intend to use it.因此,从广义上讲, &str会更节省空间,但生命周期会引入更多约束,这可能会使StrSliceVector<'a>不可行,具体取决于您打算如何使用它。 Since references don't own their data, you must ensure that whatver does own the strings keeps them alive as long as you are using the vector.由于引用不拥有它们的数据,因此只要您使用向量,您必须确保拥有字符串的任何内容都可以使它们保持活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM