InfiniTensor 是基于清华大学高性能所科研团队建立的开源组织,
主要研究领域包括人工智能编译器、大模型推理系统和国产硬件的生态建设。
rustlings是一个rustOJ形式的学习平台,通过90多道题目来测试rust语法的掌握程度,第一次接触的时候会感觉非常新颖,通过rustlings进行学习也非常高效。
我的任务:
学员晋级条件:
学员在基础阶段可选Rust基础或C++基础完成习题,将一个方向的习题完成并满分即可晋级专业阶段。
我直接ai完成这个题目。
Installing Rust
Rust开发时有时使用官方的源太慢,可以考虑更换使用国内的源
字节跳动搞的,希望能帮助建设国内的 rust 生态,感兴趣的话可以试用,无任何限速(实际上是 1000Gbps)。
地址:https://rsproxy.cn/
## rust开发环境设置
步骤一:设置 Rustup 镜像
export RUSTUP_DIST_SERVER="https://rsproxy.cn"
export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup"
source ~/.bashrc
步骤二:安装 Rust
curl --proto '=https' --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh
检查安装是否成功
rustc -V
rustc 1.86.0-nightly (4a4309466 2025-02-02)
rustup component add clippy
rustup component add rustfmt
检查安装是否成功Rust 本地文档的使用:rustup docRust 是一种系统级编程语言,以其安全性、速度和内存控制能力而闻名。为了方便开发者更好地了解并利用 Rust 标准库和工具链中的功能,Rust 提供了一种内置的文档浏览方式——通过 rustup doc 命令安装 Rust 的同时也会在本地安装一个文档服务 ,方便我们离线阅读:运行 rustup doc 让浏览器打开本地文档。
rustup component add --toolchain nightly-x86_64-apple-darwin rust-docs
rustup doc
Rust官方提供了完善的开发工具链。
简单介绍如下:
来源:
如果你想直接打开特定组件(如标准库)的文档:
rustup doc std
若要查看 Rust 语言参考(The Rust Reference):
rustup doc reference
开发工具:
Our general recommendation is VS Code with the rust-analyzer plugin.
参考资料:
按此教程操作,完成构建自己的评测系统用于后续阶段的测验提交,并熟悉评测系统的使用。
下列步骤完成了 3 项工作:
Git 的安装可参考:
1.5 起步 - 安装 Git 或 InfiniTensor 训练营导学阶段指导书
~/.gitconfig
[core]
autocrlf = input
[user]
email = wang_cyi@163.com
name = watchpoints
[credential]
学员需要通过 exam-grading模板 创建自己的评分系统仓库:
NOTICE 推荐创建为private仓库
之后将创建的exam-grading仓库拉取到本地
git clone <repo-addr> ./exam-grading
cd exam-grading
git clone git@github.com:watchpoints/rust-learn.git ./exam-grading
同样按照[以上步骤](#步骤)的前两步准备环境和创建仓库并克隆到本地,之后在 exam-grading
项目根目录中添加测试目录:
# 将learning-cxx克隆到exam-grading目录外(也就是exam-grading的同级目录)
git clone <target-test-repo> ./learning-cxx
cd learning-cxx
# 确保为最新
git pull
# 然后将目录复制进exam-grading
# 将rustlings克隆到exam-grading目录外(也就是exam-grading的同级目录)
git clone <target-test-repo> ./rustlings
git clone git@github.com:watchpoints/rustlings-zh-cn.git ./rustlings
cd rustlings
# 确保为最新
git pull
# 然后将目录复制进exam-grading
https://github.com/cicvedu/rustlings-semester-4-watchpoints/tree/main/exercises
NOTICE 目标测试目录中存在 .git 目录可能会导致问题,删除后再试
# learning-cxx
git submodule add <target-test-repo> ./learning-cxx
# rustlings
git submodule add <target-test-repo> ./rustlings
# 确保为最新
git submodule update --remote
NOTICE
learning-cxx
和rustlings
目录名写死,否则检测不到,不会执行对应评分。
https://rustlings.cool/
rustlings init
cd rustlings
# 确保为最新
git pull
# 然后将目录复制进exam-grading
直接目录:
# 将 TinyInfiniTensor 克隆到 exam-grading 目录外(也就是 exam-grading 的同级目录)
git clone <target-test-repo> ./TinyInfiniTensor
cd TinyInfiniTensor
# 确保为最新
git pull
# 然后将目录复制进 exam-grading
# 将 learning-lm-rs 克隆到 exam-grading 目录外(也就是 exam-grading 的同级目录)
git clone <target-test-repo> ./learning-lm-rs
cd learning-lm-rs
# 确保为最新
git pull
# 然后将目录复制进 exam-grading
NOTICE 目标测试目录中存在 .git 目录可能会导致问题,删除后再试
子模块:
# TinyInfiniTensor
git submodule add <target-test-repo> ./TinyInfiniTensor
# learning-lm-rs
git submodule add <target-test-repo> ./learning-lm-rs
# 确保为最新
git submodule update --remote
NOTICE
TinyInfiniTensor
和learning-lm-rs
目录名写死,否则检测不到,不会执行对应评分。
可在子模块内直接做题,之后先提交子模块仓库,再提交 exam-grading
的即可。
也可选择在其它位置完成题目后,通过直接目录形式复制进 exam-grading
提交评分,
或通过子模块方法添加目录后提交评分。
做题顺序
rustlings run
Option
枚举类型来实现https://rustwiki.org/zh-CN/rust-by-example/std/option.html
ref
?场景 | 匹配方式 | 是否移动所有权 |
---|---|---|
Some(p) | match optional_point | ✅ 移动 |
Some(ref p) | match optional_point | ❌ 不移动 |
Some(p) | match &optional_point | ❌ 不移动 |
在 Rust 的 match
语句中,ref
关键字的作用是 创建引用,而不会移动所有权。 让我们详细拆解 Some(ref p)
的含义。
ref
?在 match
语句中,默认情况下模式匹配会 移动 变量的所有权。例如:
let optional_point = Some(Point { x: 100, y: 200 });
match optional_point {
Some(p) => println!("Co-ordinates are {},{}", p.x, p.y), // ❌ 这里会移动
Point
_ => panic!("No match!"), } println!("{optional_point:?}"); // ❌ 编译错误!optional_point 已被移动
编译错误的原因:
Some(p)
会将 Point
的所有权移动 给 p
,导致 optional_point
失效。optional_point
仍然在 println!
语句中使用,Rust 不允许使用已被移动的变量。ref
关键字match optional_point {
Some(ref p) => println!("Co-ordinates are {},{}", p.x, p.y), // ✅ 这里
p
只是引用,不移动 _ => panic!("No match!"), }
Some(ref p)
的作用:
ref p
不会移动 Point
,而是创建一个 Point
的引用 p
。optional_point
仍然保持所有权,可以在 println!
语句中继续使用。ref
的等价写法其实,我们也可以使用 match
绑定一个引用,而不使用 ref
:
match &optional_point {
Some(p) => println!("Co-ordinates are {},{}", p.x, p.y), // ✅ 这里
p
直接是引用 _ => panic!("No match!"), }
区别:
match optional_point
直接匹配 Some(ref p)
(p
是 Point
的引用)。match &optional_point
直接匹配 Some(p)
,因为 &optional_point
本身就是 &Option<Point>
。ref
?场景 | 匹配方式 | 是否移动所有权 |
---|---|---|
Some(p) | match optional_point | ✅ 移动 |
Some(ref p) | match optional_point | ❌ 不移动 |
Some(p) | match &optional_point | ❌ 不移动 |
推荐:
Some(ref p)
或 match &optional_point
。match &optional_point
更符合现代 Rust 习惯,推荐使用。#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let optional_point = Some(Point { x: 100, y: 200 });
// 使用
match &optional_point
代替Some(ref p)
match &optional_point { Some(p) => println!("Co-ordinates are {},{}", p.x, p.y), _ => panic!("No match!"), } println!("{optional_point:?}"); // ✅ 仍然可以使用 }
运行结果:
Co-ordinates are 100,200
Some(Point { x: 100, y: 200 })
🚀 总结:
Some(ref p)
在匹配时借用值,而不是移动。match &optional_point
,更加符合 Rust 习惯。有时候想要捕捉到程序某部分的失败信息,而不是调用 panic!
;
这可使用 Option
枚举类型来实现。
Option<T>
有两个变量:
None
,表明失败或缺少值Some(value)
,元组结构体,封装了一个 T
类型的值 valu
use std::fs::File;
fn main() {
let greeting_file_result = File::open("hello.txt");
let greeting_file = match greeting_file_result {
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {error:?}"),
};
}
Generics is the topic of generalizing types and functionalities to broader cases.
This is extremely useful for reducing code duplication in many ways, but can call for some rather involved syntax.
Namely, being generic requires taking great care to specify over which types a generic type is actually considered valid.
The simplest and most common use of generics is for type parameters.
A trait is a collection of methods.
Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the String
data type implements the From<&str>
trait. This allows a user to write String::from("hello")
.
In this way, traits are somewhat similar to Java interfaces and C++ abstract classes.
Some additional common Rust traits include:
Clone
(the clone
method)Display
(which allows formatted display via {}
)Debug
(which allows formatted display via {:?}
)Because traits indicate shared behavior between data types, they are useful when writing generics.
Lifetimes tell the compiler how to check whether references live long
enough to be valid in any given situation. For example lifetimes say
"make sure parameter 'a' lives as long as parameter 'b' so that the return
value is valid".
They are only necessary on borrows, i.e. references,
since copied parameters or moves are owned in their scope and cannot
be referenced outside. Lifetimes mean that calling code of e.g. functions
can be checked to make sure their arguments are valid. Lifetimes are
restrictive of their callers.
If you'd like to learn more about lifetime annotations, the
lifetimekata project
has a similar style of exercises to Rustlings, but is all about
learning to write lifetime annotations.
This section will teach you about Iterators.
In Rust, smart pointers are variables that contain an address in memory and reference some other data, but they also have additional metadata and capabilities.
Smart pointers in Rust often own the data they point to, while references only borrow data.
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有