basic/collections/vector #727
Replies: 23 comments 31 replies
-
感觉这里的内容不够全 |
Beta Was this translation helpful? Give feedback.
-
有个问题:动态数组,默认会分多大内存,为了避免频繁的申请内存,应该有个默认值吧,还有请问,每次push的时候都会重新分配内存吗,golang的内存分配不是这样的,如果不是重新分配内存,push时,原来的地址还是有效的,麻烦仔细讲一下这个地方。 |
Beta Was this translation helpful? Give feedback.
-
我的意思是,你可以把这个地方讲细一点,我知道push不会每次都分配内存,当len=cap的时候才会,而且内存分配分两种情况,一种是grow,一种重新分配。而且你说的原因在于,内存重新分配导致那个问题,不是很正确,就算不重新分配,也会编译不过,与所有权有关系。 |
Beta Was this translation helpful? Give feedback.
-
要修改得是i,就算要声明,不应该是&mut i吗 |
Beta Was this translation helpful? Give feedback.
-
确实,可能是思维惯势
…------------------ 原始邮件 ------------------
发件人: ***@***.***>;
发送时间: 2022年6月22日(星期三) 晚上9:39
收件人: ***@***.***>;
抄送: ***@***.***>; ***@***.***>;
主题: Re: [sunface/rust-course] basic/collections/vector (Discussion #727)
为什么不直接使用 let third: i32 = v[2]; 而使用 let third: &i32 = &v[2]; 呢?这个 vector 里面的 值友 clone 属性,不会发生权的转移吧
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
在实际使用场景中,特征对象数组要比枚举数组常见很多,主要原因在于特征对象非常灵活,而编译器对枚举的限制较多,且无法动态增加类型。 |
Beta Was this translation helpful? Give feedback.
-
特征对象的实现也可以使用&dyn let v1 = V6(String::from("::1"));
let v2 = V4(String::from("127.0.0.1"));
let v: Vec<&dyn IpAddr> = vec![&v1, &v2];
for ip in v {
ip.display();
} |
Beta Was this translation helpful? Give feedback.
-
我觉得本页的内容我都学会了,挺舒服的,看了评论区这种感觉就又没了,哎 |
Beta Was this translation helpful? Give feedback.
-
”数组的大小是可变的,当旧数组的大小不够用时,Rust 会重新分配一块更大的内存空间,然后把旧数组拷贝过来。这种情况下,之前的引用显然会指向一块无效的内存“ let mut v1 = Vec::with_capacity(1);
v1.push(0);
println!("addr: {:p}", &v1);
v1.push(1);
println!("addr: {:p}", &v1); 输出:
|
Beta Was this translation helpful? Give feedback.
-
"先不运行,来推断下结果,首先 first = &v[0] 进行了不可变借用,v.push 进行了可变借用,如果 first 在 v.push 之后不再使用,那么该段代码可以成功编译" |
Beta Was this translation helpful? Give feedback.
-
“答案很简单,当你确保索引不会越界的时候,就用索引访问,否则用 .get。” |
Beta Was this translation helpful? Give feedback.
-
貌似链接失效了,Document not found (404) |
Beta Was this translation helpful? Give feedback.
-
有几个问题:
代码片段2可以通过编译通过:
这里我有几个问题:
问题3.Rust的下标索引是一个语法糖,底层是调用的Index::index方法
根据方法定义和生命周期消除规则的第3条,方法返回值和&self的生命周期是一致的,为什么片段2的f不受此影响。理论上说,片段1就是因为方法返回值和&self的生命周期是一致导致的 |
Beta Was this translation helpful? Give feedback.
-
总体而言,非稳定 排序的算法的速度会“由于” 稳定 排序算法,同时,稳定 排序还会额外分配原数组一半的空间。 优于? |
Beta Was this translation helpful? Give feedback.
-
希望给Person排序的那一段可以用 |
Beta Was this translation helpful? Give feedback.
-
一开始看的时候还有点疑惑,其实这里的 “特性” 指的就是之前的 “特征” Trait 对吧? |
Beta Was this translation helpful? Give feedback.
-
rust没有函数重载吗,为什么不用new(10)来创建一个容量为10的数组,而用with_capacity呢? |
Beta Was this translation helpful? Give feedback.
-
浮点数数组的排序, 不包含 NAN 值,那么我们可以使用 partial_cmp 来作为大小判断的依据。 这里是不是要把不去掉,前面都说了存在一个 NAN 的值,还是我理解错了 |
Beta Was this translation helpful? Give feedback.
-
想请教一下各位大神下面这个问题,按报错的说法,只打印v的时候也是有mutable借用和immutable借用吧?为什么把a也打印出来就会报错,不打印a就不报错?
|
Beta Was this translation helpful? Give feedback.
-
感觉还是得介绍下 |
Beta Was this translation helpful? Give feedback.
-
在 Rust 中,Vec::with_capacity(3) 创建一个具有足够空间存放 3 个元素的 Vec,但这并不意味着你不能添加更多的元素。这里的容量只是初始时预分配的内存大小,当超过这个容量时,Vec 会自动增加其容量来容纳更多元素。 |
Beta Was this translation helpful? Give feedback.
-
不理解EQ和Ord的可以看看这篇博客 |
Beta Was this translation helpful? Give feedback.
-
感觉这里报错和 Vec 扩容不扩容没什么关系, 即使 Vec 没有扩容操作也会报错,这里我感觉就是纯纯的 let mut v = vec![1, 2, 3, 4, 5];
let first = &v[0];
v.push(6);
println!("{:?}", first); |
Beta Was this translation helpful? Give feedback.
-
basic/collections/vector
https://course.rs/basic/collections/vector.html
Beta Was this translation helpful? Give feedback.
All reactions