100个练习学习Rust!一个简单有意思的学习rust的网站

发现了一个很好的学习rust的新手入门网站,与传统学习代码的过程不同的是,这里的实践练习非常多,字如其名,100个练习学习Rust!接下来我会记录我学习这个网站的过程

环境搭建

URL:GitHub - mainmatter/100-exercises-to-learn-rust: A self-paced course to learn Rust, one exercise at a time.

搭建rust的过程就不写了,网上教程非常多,首先将它克隆到本地

# 如果你已经为GitHub设置了SSH密钥
git clone git@github.com:mainmatter/100-exercises-to-learn-rust.git

# 否则,使用HTTPS链接:
git clone https://github.com/mainmatter/100-exercises-to-learn-rust.git

同时可以安装配套的wr工具:workshop-runner

打开新终端并导航到存储库的顶级文件夹。 运行命令以启动课程:wr

这个工具可以帮助你管理进度,解答完问题后可以使用它来更新进度。

出现类似信息就是成功,我用的是RustRover,也可选择vscode+rust-analyzer

如果跟随我的教程无法成功,也可以去官方网站下,同样拥有安装教程与指引。

尝试挑战

再输入wr后,输入y进入第一关挑战

$ wr
 
 
Running tests...
 
        Eternity lies ahead of us, and behind. Your path is not yet finished. 🍂
 
Do you want to open the next exercise, (01) intro - (00) welcome? [y/n] y
 
 
        Ahead of you lies (01) intro - (00) welcome
 
        Open "exercises/01_intro/00_welcome" in your editor and get started!
        Run `wr` again to compile the exercise and execute its tests.

打开100-exercises-ro-learn-rust/exercises/01_intro/00_welcome/src/lib.rs看到要完成的第一个调整

 注:看到TODO等类似字样就是要修改的代码部分,或者查看注释中的题目要求和提示也都可以快速完成挑战,如果实在无法完成也可以寻找官方solution分支上的正确答案or关注我,我会尽力完成下面的100个挑战(目前只完成了一半,等我学完会一次发出)

第一关代码如下,读完注释发现很简单,assert_eq!就是一个比较函数,只需要把__修改为Rust就可以通过了

// This is a Rust file. It is a plain text file with a `.rs` extension.
//
// Like most modern programming languages, Rust supports comments. You're looking at one right now!
// Comments are ignored by the compiler; you can leverage them to annotate code with notes and
// explanations.
// There are various ways to write comments in Rust, each with its own purpose.
// For now we'll stick to the most common one: the line comment.
// Everything from `//` to the end of the line is considered a comment.
 
// Exercises will include `TODO`, `todo!()` or `__` markers to draw your attention to the lines
// where you need to write code.
// You'll need to replace these markers with your own code to complete the exercise.
// Sometimes it'll be enough to write a single line of code, other times you'll have to write
// longer sections.
//
// If you get stuck for more than 10 minutes on an exercise, grab a trainer! We're here to help!
// You can also find solutions to all exercises in the `solutions` git branch.
fn greeting() -> &'static str {
    // TODO: fix me 👇
    "I'm ready to __!"
}
 
// Your solutions will be automatically verified by a set of tests.
// You can run these tests directly by invoking the `cargo test` command in your terminal,
// from the root of this exercise's directory. That's what the `wr` command does for you
// under the hood.
//
// Rust lets you write tests alongside your code.
// The `#[cfg(test)]` attribute tells the compiler to only compile the code below when
// running tests (i.e. when you run `cargo test`).
// You'll learn more about attributes and testing later in the course.
// For now, just know that you need to look for the `#[cfg(test)]` attribute to find the tests
// that will be verifying the correctness of your solutions!
//
// ⚠️ **DO NOT MODIFY THE TESTS** ⚠️
// They are there to help you validate your solutions. You should only change the code that's being
// tested, not the tests themselves.
#[cfg(test)]
mod tests {
    use crate::greeting;
 
    #[test]
    fn test_welcome() {
        assert_eq!(greeting(), "I'm ready to learn Rust!");
    }
}

修改完后再命令行继续打出wr

$ wr
 
 
Running tests...
 
        🚀 (01) intro - (00) welcome
        Eternity lies ahead of us, and behind. Your path is not yet finished. 🍂
 
Do you want to open the next exercise, (01) intro - (01) syntax? [y/n] y
 
        Ahead of you lies (01) intro - (01) syntax
 
        Open "exercises/01_intro/01_syntax" in your editor and get started!
        Run `wr` again to compile the exercise and execute its tests.

就是通过挑战了!

posted @ 2025-04-13 11:59  ModesL  阅读(326)  评论(0)    收藏  举报