Replies: 76 comments 93 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.
-
对应的习题可能超前了。这一节没有 String 类型,只有 &str // 修复错误
fn main() {
println!("{}, world", x);
}
fn define_x() {
let x = "hello";
} |
Beta Was this translation helpful? Give feedback.
-
我用rust开发了一个java虚拟机demo |
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.
-
我来说一个点,貌似没看到人提到 fn main() {
let x = 5;
// 在main函数的作用域内对之前的x进行遮蔽
let x = x + 1;
{
// 在当前的花括号作用域内,对之前的x进行遮蔽
let x = x * 2;
println!("The value of x in the inner scope is: {}", x);
}
println!("The value of x is: {}", x);
} 这里面的第二个x,是重新分配了内存了吗?因为文中说let是重新分配内存了。 |
Beta Was this translation helpful? Give feedback.
-
果然难学,光一个变量整出这么多花样来 |
Beta Was this translation helpful? Give feedback.
-
你这个写法有问题。 rust 中变量默认是不可变的。 因此 let a = 10; 之后 a = 20 是违背语法的
kerongw ***@***.***> 于2024年9月12日周四 17:25写道:
… 果然难学,光一个变量整出这么多花样来
let a = 10;
a = 20;
let b = 10;
let b = 20;
let mut c = 10;
c = 20;
—
Reply to this email directly, view it on GitHub
<#613 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFQKY2LSP24SV2DR5EQ3QADZWFMZLAVCNFSM5RW37LJ2U5DIOJSWCZC7NNSXTOSENFZWG5LTONUW63SDN5WW2ZLOOQ5TCMBWGI2DANJW>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
常量既然值不能变了,类型可以推导出来吧,为什么还必须定义类型呢?rust这样设计有什么目的吗 |
Beta Was this translation helpful? Give feedback.
-
rust咋不都像js一样用var、let、const替代呢。 |
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.
-
https://course.rs/basic/variable.html
Beta Was this translation helpful? Give feedback.
All reactions