Replies: 86 comments 102 replies
-
|
这里容易误导新人,一开始是变量不可变避免运行时检查从而提高性能,,后面又说不可变丧失性能,这里其实是矛盾的,没有说清楚 |
Beta Was this translation helpful? Give feedback.
-
确实,这里没有讲清楚,感谢建议 :) |
Beta Was this translation helpful? Give feedback.
-
|
字符串类型的这一小节,建议替换一下 fn main () { |
Beta Was this translation helpful? Give feedback.
-
|
好家伙,解构式赋值那块的 |
Beta Was this translation helpful? Give feedback.
-
|
Struct { e, .. } = Struct { e: 5 };这个绑定怎么理解呢? |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
不可变可以带来安全性,但是丧失了灵活性和性能。 如果上面这段话表达没有问题,能否详细展开说明:
|
Beta Was this translation helpful? Give feedback.
-
|
fn main() { } struct Struct { 结构体解构赋值怎么忽略字段呢? |
Beta Was this translation helpful? Give feedback.
-
|
变量和常量还有一个区别,变量在栈stack上动态分配,而常量分配在.data静态数据段上,常量会一次性静态加载,使用时直接寻址,性能会更好。 |
Beta Was this translation helpful? Give feedback.
-
|
我的理解,这里let和let mut的关系,就相当于其他语言的var和const关系,是常见的特性,不知道对吗 |
Beta Was this translation helpful? Give feedback.
-
|
作为一个 ts 程序员,平时写代码,变量大部分都是 const 来定义,只有在真的需要重新赋值的地方才用 let,我觉得这和 let 与 let mut 差不多的思路。 |
Beta Was this translation helpful? Give feedback.
-
|
课后练习精心设计,很棒~ |
Beta Was this translation helpful? Give feedback.
-
|
为什么我照例子写的,执行cargo run,非但没报错,还正确打印出来了, |
Beta Was this translation helpful? Give feedback.
-
struct Struct {
e: i32
}
fn main() {
let (a, b, c, d, e);
(a, b) = (1, 2);
// _ 代表匹配一个值,但是我们不关心具体的值是什么,因此没有使用一个变量名而是使用了 _
[c, .., d, _] = [1, 2, 3, 4, 5];
Struct { e, .. } = Struct { e: 5 };
assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]);
}我不理解这个解构式赋值,请问何为“解构”呢,在赋值语句哪里中括号“[]”和小括号“()”有什么区别吗,“...”又是做什么用的呢 , 下划线"_"说是匹配一个值,在上面的例子代码中匹配的是 e 吗? |
Beta Was this translation helpful? Give feedback.
-
|
写得太棒了! |
Beta Was this translation helpful? Give feedback.
-
|
"但是 let 会重新绑定,而这里仅仅是对之前绑定的变量进行再赋值。"这句话应该是 |
Beta Was this translation helpful? Give feedback.
-
|
变量和值互相绑定,就像狐狸和小王子互相拥有一样 |
Beta Was this translation helpful? Give feedback.
-
|
rustc 1.82.0 (f6e511eec 2024-10-15) |
Beta Was this translation helpful? Give feedback.
-
|
我有一些疑问,遮蔽变量之后,之前的变量我们无法访问,就意味着之前的变量我们不再需要,那么还存放在栈中是不是会增加内存的开销呢? |
Beta Was this translation helpful? Give feedback.
-
|
「课后练习」好评 |
Beta Was this translation helpful? Give feedback.
-
|
常量似乎也能遮蔽 fn main() {
const A:i32=123;
println!("{}",A);
{
const A:&str="hello";
println!("{}",A);
}
}输出: |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
坏了,我不是目标读者 |
Beta Was this translation helpful? Give feedback.
-
|
mut可以用于变量赋值表达式左侧。用于右侧的时候只能是 |
Beta Was this translation helpful? Give feedback.
-
|
不太能理解为什么设计遮蔽这样的东西,虽然命名简单了,但是看代码的时候会觉得乱,得就近匹配变量在当前的context下是什么含义。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
这答非所问
发自我的iPhone
…------------------ Original ------------------
From: 布里焦焦迪布利多 ***@***.***>
Date: Wed,Dec 31,2025 10:09 AM
To: sunface/rust-course ***@***.***>
Cc: Raven ***@***.***>, Comment ***@***.***>
Subject: Re: [sunface/rust-course] basic/variable (Discussion #613)
感觉奇怪就不用这个特性就好了呀
—
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.
-
|
这里看错了。理解成了不变量有检查。
发自我的iPhone
…------------------ Original ------------------
From: 布里焦焦迪布利多 ***@***.***>
Date: Wed,Dec 31,2025 10:10 AM
To: sunface/rust-course ***@***.***>
Cc: Raven ***@***.***>, Comment ***@***.***>
Subject: Re: [sunface/rust-course] basic/variable (Discussion #613)
你为啥觉得声明为可变能避免运行时检查?
—
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.
-
|
有个问题请教,我的理解是 struct A {
s: Vec<u8>,
}
fn main() {
let a = A { s: Vec::new() };
a.s.push(3u8);//错误
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://course.rs/basic/variable.html
Beta Was this translation helpful? Give feedback.
All reactions