做网站 推广,国内商务网络公司排名,网页设计怎样做一个网页,手机wap网站怎么做公司内部的学习会非常活跃#xff01;我也参与了Rust学习会#xff0c;并且一直在研究rustlings。最近#xff0c;我发现了一个类似于rustlings的新教程网站#xff1a;Welcome - 100 Exercises To Learn Rust。
rustlings是基于Rust的权威官方文档《The Rust Programming…公司内部的学习会非常活跃我也参与了Rust学习会并且一直在研究rustlings。最近我发现了一个类似于rustlings的新教程网站Welcome - 100 Exercises To Learn Rust。
rustlings是基于Rust的权威官方文档《The Rust Programming Language》简称TRPL制作的而100-exercises则在特性和异步处理等方面深入探讨比TRPL更具挑战性真的很值得一试
接下来的一段时间我会挑战“100 Exercises To Learn Rust”并将这个过程记录下来写成文章。那么让我们开始今天的准备篇吧
环境搭建
这次我选择在 Ubuntu 24.10 上进行挑战安装方法可以在官方网站上查看。
https://www.rust-lang.org/zh-CN/tools/install 不过这其实一点也不难。Rust的安装非常简单只需一行命令就可以完成而且不需要管理员权限
curl --proto https --tlsv1.2 -sSf https://sh.rustup.rs | shinfo: downloading installerWelcome to Rust!~~ 省略 ~~Current installation options:default host triple: x86_64-unknown-linux-gnudefault toolchain: stable (default)profile: defaultmodify PATH variable: yes1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation在这里选择1。
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) Cancel installation
1~~ 省略 ~~stable-x86_64-unknown-linux-gnu installed - rustc 1.78.0 (9b00956e5 2024-04-29)Rust is installed now. Great!To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargos bin directory ($HOME/.cargo/bin).To configure your current shell, you need to source
the corresponding env file under $HOME/.cargo.This is usually done by running one of the following (note the leading DOT):
. $HOME/.cargo/env # For sh/bash/zsh/ash/dash/pdksh
source $HOME/.cargo/env.fish # For fish如果你想在当前终端直接使用 cargo 命令等需要加载 ~/.cargo/env 文件。顺便也可以检查一下版本。
. $HOME/.cargo/env
cargo --version输出内容
cargo 1.78.0 (54d8815d0 2024-03-26)据说本月即将发布的1.80版本会有很多有趣的功能比如cargo-script等。不过现在我们还是以1.78.0版本为前提来进行接下来的学习吧
虽然到这里看似准备工作已经完成了……但在Rust的编译过程中我们还需要用到像gcc等C语言的编译环境。如果你是从一个全新安装的Ubuntu系统开始的话可能会遇到如下错误。
$ cargo runCompiling project v0.1.0 (/path/to/project)
error: linker cc not found| note: No such file or directory (os error 2)error: could not compile project (bin project) due to 1 previous error通过使用 apt 安装 build-essential 软件包就可以安装gcc等必要的工具这样就能顺利进行编译了。
sudo apt update
sudo apt install build-essential另外在使用 reqwest这个crate时还需要安装 libssl-dev 等库。在进行Rust编程时可能还会有其他需要提前安装的包不过我们可以在练习过程中遇到需要时再逐一安装。 100 Exercises 导入
接下来我们将引入 100-exercises-to-learn-rust。这个项目是通过 git 克隆的方式获取的。
git clone https://github.com/mainmatter/100-exercises-to-learn-rust.git
cd 100-exercises-to-learn-rust
git checkout -b my-solutions这样一来main分支的内容就会下载到本地。为了方便进度管理我建议你在my-solutions分支上进行管理我也是这么做的。所有练习题都在exercises目录下。
此外据说solutions分支上有参考答案如果在某些地方遇到困难可以随时查看该分支的内容。 Workshop Runner 的安装
就像rustlings有专用的检查工具一样100 Exercises 也有用于检查的工具。你可以通过 cargo install 命令来安装这个工具。 cargo install --locked workshop-runner
wr --help输出内容
$ wr --help
A CLI to run test-driven Rust workshopsUsage: wr [OPTIONS] [COMMAND]Commands:open Open a specific exercisehelp Print this message or the help of the given subcommand(s)Options:--no-skip ...--verbose ...--keep-going ...-h, --help Print help-V, --version Print version这个工具似乎可以帮助你管理进度解答完问题后可以使用它来更新进度。
编辑器选择
业界默认的标准组合是VSCode rust-analyzer因此我也会采用这个组合。rust-analyzer在安装后无需进行特别的配置所以这里就不详细介绍了。
另外一个选择是最近正式发布的付费编辑器RustRover感觉也不错。其实我对它很感兴趣 尝试挑战 第一题[01_intro/00_welcome]
到这里环境已经顺利搭建完成现在终于可以开始解题了。输入 wr 命令并选择“y”即可进入第一道题目。
$ wrRunning 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] yAhead of you lies (01) intro - (00) welcomeOpen exercises/01_intro/00_welcome in your editor and get started!Run wr again to compile the exercise and execute its tests.打开 exercises/01_intro/00_welcome/src/lib.rs 文件你会看到一些注释其中提到以下内容
// 表示单行注释。TODO 或 todo!()以及 __用于强调你需要在练习中完成的部分。代码的检查直接使用了Rust的标准测试机制可以通过 cargo test 命令进行验证。不过wr 命令也可以替代这一过程。请不要修改测试内容。
那么让我们赶紧解决第一个问题然后继续前进吧
fn greeting() - static str {// TODO: fix me Im ready to __!
}#[cfg(test)]
mod tests {use crate::greeting;#[test]fn test_welcome() {assert_eq!(greeting(), Im ready to learn Rust!);}
}带注释的版本
// This is a Rust file. It is a plain text file with a .rs extension.
//
// Like most modern programming languages, Rust supports comments. Youre 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 well 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.
// Youll need to replace these markers with your own code to complete the exercise.
// Sometimes itll be enough to write a single line of code, other times youll have to write
// longer sections.
//
// If you get stuck for more than 10 minutes on an exercise, grab a trainer! Were here to help!
// You can also find solutions to all exercises in the solutions git branch.
fn greeting() - static str {// TODO: fix me Im 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 exercises directory. Thats 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).
// Youll 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 thats being
// tested, not the tests themselves.
#[cfg(test)]
mod tests {use crate::greeting;#[test]fn test_welcome() {assert_eq!(greeting(), Im ready to learn Rust!);}
}只要修改代码使测试通过你就成功完成这一题了。
解说
这一题很简单只需要将指定的字符串按照提示修改即可
fn greeting() - static str {// TODO: fix me - Im ready to __! Im ready to learn Rust!}这里稍微解释一下assert_eq! 是一个类似于函数的宏它接受两个参数并判断这两个参数是否相等。
由于这是“字符串的比较”对于习惯其他编程语言的人来说可能会对这个过程有所警惕但不用担心。大概在后面的章节中会提到Rust 中的 比较实际上是通过 eq 方法来实现的对于字符串比较它会从头开始逐字符进行比较确保所有字符都相等。不会出现像检查指针是否相等这种直觉上不符合预期的比较方式。
解决问题后你可以再次在项目根目录下运行 wr 命令确认修改是正确的。
$ wrRunning tests... (01) intro - (00) welcomeEternity 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] yAhead of you lies (01) intro - (01) syntaxOpen exercises/01_intro/01_syntax in your editor and get started!Run wr again to compile the exercise and execute its tests.看起来一切都顺利那我们继续挑战下一个问题吧
下一篇文章: 【1】 语法、整数、变量 文章转载自: http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.dnpft.cn.gov.cn.dnpft.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.kljhr.cn.gov.cn.kljhr.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.chgmm.cn.gov.cn.chgmm.cn http://www.morning.llyqm.cn.gov.cn.llyqm.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.xhhqd.cn.gov.cn.xhhqd.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.rcrfz.cn.gov.cn.rcrfz.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.wgdnd.cn.gov.cn.wgdnd.cn http://www.morning.mnlk.cn.gov.cn.mnlk.cn http://www.morning.ygkk.cn.gov.cn.ygkk.cn http://www.morning.nlzpj.cn.gov.cn.nlzpj.cn http://www.morning.fqzz3.cn.gov.cn.fqzz3.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.mcpby.cn.gov.cn.mcpby.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.zckhn.cn.gov.cn.zckhn.cn http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.yymlk.cn.gov.cn.yymlk.cn http://www.morning.yrck.cn.gov.cn.yrck.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.wbnsf.cn.gov.cn.wbnsf.cn http://www.morning.pszw.cn.gov.cn.pszw.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.gbfck.cn.gov.cn.gbfck.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.fcxt.cn.gov.cn.fcxt.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.lsmnn.cn.gov.cn.lsmnn.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.hfnbr.cn.gov.cn.hfnbr.cn http://www.morning.rnytd.cn.gov.cn.rnytd.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.jpbky.cn.gov.cn.jpbky.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.frxsl.cn.gov.cn.frxsl.cn http://www.morning.mehrim.com.gov.cn.mehrim.com http://www.morning.sphft.cn.gov.cn.sphft.cn http://www.morning.qxdrw.cn.gov.cn.qxdrw.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn