-
Notifications
You must be signed in to change notification settings - Fork 1.1k
variables #181
-
variables
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 122 -
😄 4 -
🎉 27 -
❤️ 19 -
🚀 14 -
👀 3
Replies: 164 comments 26 replies
-
mark一下
Beta Was this translation helpful? Give feedback.
All reactions
-
mark一下-2022年4月14日
Beta Was this translation helpful? Give feedback.
All reactions
-
第一题感觉这样,更容易理解 println!("x is equal to {}", x);
Beta Was this translation helpful? Give feedback.
All reactions
-
好像、确实如此🤣 可以提个 PR :D
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
hhhhh
Beta Was this translation helpful? Give feedback.
All reactions
-
没看到答案呢
Beta Was this translation helpful? Give feedback.
All reactions
-
fn main() {
let x: i32 = 5; // uninitialized but using, ERROR !
let _y: i32;
println!("{} is equal to 5", x);
}
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 2
-
打卡
Beta Was this translation helpful? Give feedback.
All reactions
-
完成,9/9
Beta Was this translation helpful? Give feedback.
All reactions
-
打开
Beta Was this translation helpful? Give feedback.
All reactions
-
🚀 1
-
🔥 整个 rust-by-practice 所有题目,哪里有答案呢?
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2 -
👀 1
-
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
thx!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
mark
Beta Was this translation helpful? Give feedback.
All reactions
-
作业已完成。花了15分钟,感觉是比较基础的练习,对于rust的变量可变性,元组,数组,结构,作用域,变量的遮蔽性,还有所有权都有一定的考察,循序渐进,很有正反馈的练习作业
Beta Was this translation helpful? Give feedback.
All reactions
-
未使用变量第二种方法。。。怎么说呢,是我好奇点了一下眼睛,看到shebang就懂了。。
Beta Was this translation helpful? Give feedback.
All reactions
-
请问第4题答案中为什么需要在"hello"后面加一个to_string()呢
Beta Was this translation helpful? Give feedback.
All reactions
-
😕 1
-
变量所有权的问题
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
因为答案的函数返回的值是String类型,而"hello"在Rust中是另一种类型:&str(本身是一个字符串切片)。所以,为了让返回值的类型和签名的返回类型一致,需要通过.to_string()方法将String类型转换为&str类型。
如果要返回&str的话,应该是这样的:
fn main() {
println!("{}, world", define_x());
}
fn define_x<'a>() -> &'a str {
let x = "hello";
x
}
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5
-
好的我懂了,谢谢两位
Beta Was this translation helpful? Give feedback.
All reactions
-
fn define_x<'a>() -> &'a str {} 这个函数格式比较难懂
ChatGPT
我理解你的困惑。让我更详细地解释一下这个函数的格式。
rust
Copy code
fn define_x<'a>() -> &'a str {}
在 Rust 中,这个函数的声明使用了泛型和生命周期参数,可能会让它看起来比较复杂。我会分步解释每个部分的含义:
fn: 这是 Rust 中定义函数的关键字。
define_x: 这是函数的名称。
<'a>: 这是一个泛型参数声明,'a 是一个生命周期标识符。生命周期参数用来指示变量或引用的有效范围。
(): 这是函数的参数列表,没有参数,所以是一个空括号。
->: 这表示函数返回类型的定义即将开始。
&'a str: 这是函数的返回类型。它表示一个生命周期为 'a 的字符串引用。在 Rust 中,&str 表示字符串切片,而 &'a str 表示具有生命周期 'a 的字符串切片。
综合起来,函数 define_x 声明了一个没有参数的函数,返回一个具有生命周期 'a 的字符串切片引用。这个生命周期参数 'a 意味着函数返回的引用必须在 'a 这个生命周期内保持有效。
这种函数声明的主要目的是确保函数返回的引用在调用它的地方能够正确地保持有效性,并且与调用函数的上下文生命周期相匹配。生命周期是 Rust 的一种安全性机制,用于避免悬垂引用和内存安全问题。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
pass
Beta Was this translation helpful? Give feedback.
All reactions
-
这种学习方式确实很不错,实时的正反馈,感谢楼主
Beta Was this translation helpful? Give feedback.
All reactions
-
第4题第二个答案
fn main() {
let x = define_x();
println!("{:?}, world", x);
}
fn define_x() -> &'static str {
let x = "hello";
x
}
里面{:?} 和 &'static str 分别是什么意思啊?
Beta Was this translation helpful? Give feedback.
All reactions
-
(1){:?} 是实现Debug Trait后的打印格式,比一般的打印信息更丰富;
(2)&‘static str 是Rust另一个折磨人的东西:生命周期。简单地说,Rust中每个引用都必须有一个生命周期,目的是当代码涉嫌制造悬垂指针的时候,Rust能够通过各个引用的生命周期标记,来作关系检查,杜绝悬垂指针。'static 是全局生命周期的意思。我也是刚刚接触这块内容,还在学习中。
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2
-
Figo was here.
Beta Was this translation helpful? Give feedback.
All reactions
-
😄 1
-
yoo
Beta Was this translation helpful? Give feedback.
All reactions
-
mark!
Beta Was this translation helpful? Give feedback.
All reactions
-
fn main() {
let x: i32 = 10;
{
let y: i32 = 5; y是局部变量;
println!("x 的值是 {}, y 的值是 {}", x, y);
}
println!("x 的值是 {}, y 的值是 {}", x, y); 在这个作用域中找不到y。
error[E0425]: cannot find value y
in this scope
Beta Was this translation helpful? Give feedback.
All reactions
-
作用域是一个变量在程序中能够保持合法的范围
Beta Was this translation helpful? Give feedback.
All reactions
-
// 修复错误
fn main() {
let x = "hello";
define_x();
println!("{}, world", x);
}
fn define_x() {
let x = "hello";
}
main 函数中的 x 是一个局部变量,值为 "hello"。
define_x 函数中的 x 是另一个局部变量,虽然名字相同,但它与 main 函数中的 x 没有任何关系。
Beta Was this translation helpful? Give feedback.
All reactions
-
总结来说,assert_eq!(x, 12); 是一种用于确保 x 的值等于 12 的断言工具,常用于测试和调试
如果x等于12,程序正常执行;否则,程序会触发一个 panic(崩溃),并输出一条错误消息,指出两个值不相等。
Beta Was this translation helpful? Give feedback.
All reactions
-
error[E0384]: cannot assign twice to immutable variable x
--> src/main.rs:7:5
|
6 | let x = x;
| - first assignment to x
7 | x += 3;
| ^^^^^^ cannot assign twice to immutable variable 这是什么意思?
Beta Was this translation helpful? Give feedback.
All reactions
-
感觉rust特别规范,每个错误编译器都会明确指出。
Beta Was this translation helpful? Give feedback.
All reactions
-
mark一下
Beta Was this translation helpful? Give feedback.
All reactions
-
mark一下
Beta Was this translation helpful? Give feedback.
All reactions
-
mark
Beta Was this translation helpful? Give feedback.
All reactions
-
头一次做题,记录一下
Beta Was this translation helpful? Give feedback.
All reactions
-
how to show up the warning when running this:
fn main() {
let x = 1;
}
Beta Was this translation helpful? Give feedback.
All reactions
-
i solved it myself
#![deny(warnings)]
fn main() {
let x = 1;
}
// Warning: unused variable: x
Beta Was this translation helpful? Give feedback.
All reactions
-
mark一下
Beta Was this translation helpful? Give feedback.
All reactions
-
Good!
Beta Was this translation helpful? Give feedback.
All reactions
-
很好的教程与练习,使我的电脑生锈。
另外有一个可能的bug:
练习给的代码前面都有一个空行,最后一行总是被遮住一半,换了个浏览器也一样,不知道是不是只有我有这个问题,虽然可以通过敲回车解决,但强迫症还是受不了 : )
Beta Was this translation helpful? Give feedback.
All reactions
-
mark - 2025年4月16日
Beta Was this translation helpful? Give feedback.
All reactions
-
omkar was here
Beta Was this translation helpful? Give feedback.
All reactions
-
done
Beta Was this translation helpful? Give feedback.