我当时正在一本书上学习Rust分,下面的摘录让我有点抓狂:
Also note that 100 has the 101 in front of it because you need a reference to use a 102. That's because of the reason we saw above: the stack needs to know the size, and a 102 can be of any length. So we access it with a 101, a reference. The compiler knows the size of a reference's pointer, and it can then use the 101 to find where the 102 data is and read it. Also, because you use a 101 to interact with a 102, you don't own it. But a 109 is an "owned" type.
我知道对于未知大小的变量,您必须将数据放在堆上,然后在堆栈上用固定长度的指针引用它.我的困惑在于100 can be of any length这句话.
Why can't a 100 type also be of unknown length at times and require the whole reference to data on heap approach?
我知道这本书以后可能会更深入地探讨细节,但我想知道是否有人可以为我提供一些额外的背景,特别是关于上面的问题?任何关于Rust中&str
和String
类型的有用的附带细节,对于语言初学者来说都是很好的,也是非常值得赞赏的.