advance/smart-pointer/box #731
Replies: 32 comments 54 replies
-
请问,使用Box::leak后的这段内存,会有被重复使用的风险吗 |
Beta Was this translation helpful? Give feedback.
-
最后这一段话很棒! |
Beta Was this translation helpful? Give feedback.
-
两个疑问
|
Beta Was this translation helpful? Give feedback.
-
刷二遍,看的挺轻松,体会到要想rust 学的好,就得多多回顾rust的基本特性。用了一段时间后,再来看,体会真的不一样。 |
Beta Was this translation helpful? Give feedback.
-
Box::leak 多次调用的话会不会导致内存越用越多啊 |
Beta Was this translation helpful? Give feedback.
-
be better coder,希望如此 |
Beta Was this translation helpful? Give feedback.
-
想不通为什么print语句不加分号也能过编译 |
Beta Was this translation helpful? Give feedback.
-
提个问题,大佬们详解下。 fn main() {
} |
Beta Was this translation helpful? Give feedback.
-
类似于c++的 |
Beta Was this translation helpful? Give feedback.
-
怎么没有习题了? |
Beta Was this translation helpful? Give feedback.
-
Box::leak的内存在程序结束后会被回收吗?如果不会回收岂不是真的内存泄露了 |
Beta Was this translation helpful? Give feedback.
-
Box::leak,它可以消费掉 Box 并且强制目标值从内存中泄漏 |
Beta Was this translation helpful? Give feedback.
-
你好,我有个疑问 |
Beta Was this translation helpful? Give feedback.
-
最后的总结说的很好,让一头雾水的我又开始振奋了 |
Beta Was this translation helpful? Give feedback.
-
应该怎么去理解“DST 类型”。定义下面会报错,因为List 是个递归类型。它一个枚举值引用了自身。 |
Beta Was this translation helpful? Give feedback.
-
"它们都拥有一个所有者,因此受所有权规则的限制:当赋值时,发生的是所有权的转移" 这句话的意思是不是这样理解: 在堆上创建了一个字符串,内容是"dump", 并赋值给stack。 这个时候在堆上其实也有个指针指向了"stack" 这个变量? |
Beta Was this translation helpful? Give feedback.
-
当栈上数据转移所有权时,实际上是把数据拷贝了一份,最终新旧变量各自拥有不同的数据,因此所有权并未转移。 |
Beta Was this translation helpful? Give feedback.
-
总结给了我继续学习的信心 |
Beta Was this translation helpful? Give feedback.
-
对这一句话有个疑问 “而特征对象在做的就是将 DST 类型转换为固定大小类型” |
Beta Was this translation helpful? Give feedback.
-
“将arr所有权转移arr1,由于 |
Beta Was this translation helpful? Give feedback.
-
请问,这里的arr和arr1不是在栈上么,所以ler arr1 = arr不是复制了一份指针给arr1应该是浅拷贝吧。 |
Beta Was this translation helpful? Give feedback.
-
当栈上数据转移所有权时,实际上是把数据拷贝了一份,最终新旧变量各自拥有不同的数据,因此所有权并未转移。 说反了吧???我问了chatgpt,也和我理解的一样:不正确。在 Rust 中,当栈上的数据转移所有权时,并不会发生数据的拷贝。相反,所有权会从一个变量转移到另一个变量,旧变量将被认为无效。这种所有权转移的过程称为"移动语义"。通过移动语义,Rust 确保在编译时避免了数据竞争和内存安全问题。 当将所有权转移到新变量时,旧变量将被标记为无效,以防止对同一数据的并发访问。这意味着只有一个变量拥有有效的所有权,因此不存在数据的重复拷贝。这种所有权转移机制使得 Rust 能够在编译时进行内存管理,而无需依赖垃圾回收或手动内存管理。 需要注意的是,对于堆上的数据,所有权转移时会发生实际的数据拷贝。这是因为堆上的数据由指针和数据组成,所有权转移会复制指针和数据。但是,这种拷贝是针对指针和数据的,而不是针对数据的内容。 因此,在 Rust 中,所有权的转移是一种高效的机制,它确保了内存安全和并发安全,而不需要额外的运行时开销。 |
Beta Was this translation helpful? Give feedback.
-
最后一段话突然励志起来了哈哈哈 |
Beta Was this translation helpful? Give feedback.
-
这里为啥又所有权的转移 |
Beta Was this translation helpful? Give feedback.
-
正常来说 ”借用” borrow 应该是指转移所有权吧? 作者写的越来越混淆,前面几章也是这样,希望可以改进! |
Beta Was this translation helpful? Give feedback.
-
想问一下,Box解引用为什么可以转移Box对其内部持有的类型T的所有权,这是不是相当于将堆上的数据复制到栈上并释放了Box? |
Beta Was this translation helpful? Give feedback.
-
最后一句话,使我学习rust的动力 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
advance/smart-pointer/box
https://course.rs/advance/smart-pointer/box.html
Beta Was this translation helpful? Give feedback.
All reactions