简体   繁体   English

为什么str无法实现ImmutableVector特性?

[英]Why does str not implement the ImmutableVector trait?

I think it would be logical ( str is after all a vector of u8 ) and useful to be able to use the functions defined in ImmutableVector . 我认为这是合乎逻辑的(毕竟stru8的向量),并且能够使用ImmutableVector定义的功能很有用。 Does anyone have an explanation? 有人有解释吗?

I am going to paste here one answer I got in the subreddit of Rust, by erickt , in the hope that it can be useful to someone else: 我将在这里粘贴我在erickt的Rust的子目录中得到的一个答案,希望它对其他人有用:

The reason we do this is because str is a Unicode string, and the fact that the underlying storage is an implementation detail. 之所以这样做,是因为str是Unicode字符串,而底层存储是实现细节。 There are many ways at iterating over the things in a string. 有很多方法可以迭代字符串中的内容。 First , you could go over the bytes, which is what you would get with ImmutableVector. 首先,您可以遍历字节,这是通过ImmutableVector获得的。 However those bytes don't correspond to characters, since strs are UTF8, multiple bytes may make up one character. 但是,这些字节不对应于字符,因为strs为UTF8,所以多个字节可以组成一个字符。 But then there are even more choices. 但是,还有更多选择。 Some languages can perceive two characters being combined into one, and so on. 某些语言可以将两个字符组合为一个,以此类推。 Iternationalization is complicated. 国际化是复杂的。 I would suggest checking out this link that goes into way more detail about this topic. 我建议您检查一下该链接,以获取有关该主题的更多详细信息。

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

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