Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

variables #181

giscus[bot] bot announced in Book Comments
Mar 29, 2022 · 164 comments · 26 replies
Discussion options

variables

Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.

https://zh.practice.rs/variables.html

You must be logged in to vote

Replies: 164 comments 26 replies

Comment options

mark一下

You must be logged in to vote
0 replies
Comment options

mark一下-2022年4月14日

You must be logged in to vote
0 replies
Comment options

第一题感觉这样,更容易理解 println!("x is equal to {}", x);

You must be logged in to vote
2 replies
Comment options

好像、确实如此🤣 可以提个 PR :D

Comment options

hhhhh

Comment options

没看到答案呢

You must be logged in to vote
1 reply
Comment options

fn main() {
let x: i32 = 5; // uninitialized but using, ERROR !
let _y: i32;
println!("{} is equal to 5", x);
}

Comment options

打卡

You must be logged in to vote
0 replies
Comment options

完成,9/9

You must be logged in to vote
0 replies
Comment options

打开

You must be logged in to vote
0 replies
Comment options

🔥 整个 rust-by-practice 所有题目,哪里有答案呢?

You must be logged in to vote
2 replies
Comment options

Comment options

thx!

Comment options

mark

You must be logged in to vote
0 replies
Comment options

作业已完成。花了15分钟,感觉是比较基础的练习,对于rust的变量可变性,元组,数组,结构,作用域,变量的遮蔽性,还有所有权都有一定的考察,循序渐进,很有正反馈的练习作业

You must be logged in to vote
0 replies
Comment options

未使用变量第二种方法。。。怎么说呢,是我好奇点了一下眼睛,看到shebang就懂了。。

You must be logged in to vote
0 replies
Comment options

请问第4题答案中为什么需要在"hello"后面加一个to_string()呢

You must be logged in to vote
4 replies
Comment options

变量所有权的问题

Comment options

因为答案的函数返回的值是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
}

Comment options

好的我懂了,谢谢两位

Comment options

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 的一种安全性机制,用于避免悬垂引用和内存安全问题。

Comment options

pass

You must be logged in to vote
0 replies
Comment options

这种学习方式确实很不错,实时的正反馈,感谢楼主

You must be logged in to vote
0 replies
Comment options

第4题第二个答案

fn main() {
 let x = define_x();
 println!("{:?}, world", x);
}
fn define_x() -> &'static str {
 let x = "hello";
 x
}

里面{:?} 和 &'static str 分别是什么意思啊?

You must be logged in to vote
1 reply
Comment options

(1){:?} 是实现Debug Trait后的打印格式,比一般的打印信息更丰富;
(2)&‘static str 是Rust另一个折磨人的东西:生命周期。简单地说,Rust中每个引用都必须有一个生命周期,目的是当代码涉嫌制造悬垂指针的时候,Rust能够通过各个引用的生命周期标记,来作关系检查,杜绝悬垂指针。'static 是全局生命周期的意思。我也是刚刚接触这块内容,还在学习中。

Comment options

Figo was here.

You must be logged in to vote
0 replies
Comment options

yoo

You must be logged in to vote
0 replies
Comment options

mark!

You must be logged in to vote
0 replies
Comment options

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

You must be logged in to vote
5 replies
Comment options

作用域是一个变量在程序中能够保持合法的范围

Comment options

// 修复错误
fn main() {
let x = "hello";
define_x();
println!("{}, world", x);
}

fn define_x() {
let x = "hello";
}

main 函数中的 x 是一个局部变量,值为 "hello"。
define_x 函数中的 x 是另一个局部变量,虽然名字相同,但它与 main 函数中的 x 没有任何关系。

Comment options

总结来说,assert_eq!(x, 12); 是一种用于确保 x 的值等于 12 的断言工具,常用于测试和调试
如果x等于12,程序正常执行;否则,程序会触发一个 panic(崩溃),并输出一条错误消息,指出两个值不相等。

Comment options

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 这是什么意思?

Comment options

感觉rust特别规范,每个错误编译器都会明确指出。

Comment options

mark一下

You must be logged in to vote
0 replies
Comment options

mark一下

You must be logged in to vote
0 replies
Comment options

mark

You must be logged in to vote
0 replies
Comment options

头一次做题,记录一下

You must be logged in to vote
0 replies
Comment options

how to show up the warning when running this:
fn main() {
let x = 1;
}

You must be logged in to vote
1 reply
Comment options

i solved it myself

#![deny(warnings)]
fn main() {
let x = 1;
}

// Warning: unused variable: x

Comment options

mark一下

You must be logged in to vote
0 replies
Comment options

Good!

You must be logged in to vote
0 replies
Comment options

很好的教程与练习,使我的电脑生锈。
另外有一个可能的bug:
练习给的代码前面都有一个空行,最后一行总是被遮住一半,换了个浏览器也一样,不知道是不是只有我有这个问题,虽然可以通过敲回车解决,但强迫症还是受不了 : )

You must be logged in to vote
0 replies
Comment options

mark - 2025年4月16日

You must be logged in to vote
0 replies
Comment options

omkar was here

You must be logged in to vote
0 replies
Comment options

done

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /