Skip to content

commit #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
## 第一期 Rust 入门训练营-基础阶段
## 2025年春季第一期 Rust 入门训练营-导学阶段基础测试

**请在完成本实验后凭借排行榜分数截图练习班主任加入学习交流群**


基础阶段实验同样将通过Rustlings进行测试,请按照以下步骤进行练习:
导学阶段将通过 Rustlings 进行测试,我们选取了 Rustlings 练习的前16道基础题目进行测试,确保您具备基本的编程能力,请按照以下步骤进行练习:

1. 在网络浏览器中用自己的 github id 登录 github.com。
2. **fork 本仓库**,并按照下述引导进行答题,**在完成全部题目后请凭借排行榜成绩截图联系班主任进入专业阶段学习群**:
2. 请将本仓库 fork 到您的 github 账号下,然后参照以下步骤完成环境配置和实验提交:
* 本地环境:
1. **安装Linux的环境**。对于windows的用户,推荐使用wsl2安装Ubuntu 22.04,也可以使用vmware等虚拟机进行安装。如果在这一步存在问题,请联系助教。
2. **创建ssh key,用于ssh方式克隆github代码**。在linux环境下,使用`ssh-keygen -t rsa -b 4096 -C "你的邮箱"`命令,创建ssh key,下面的选项全部直接敲回车即可。 随后使用` cat ~/.ssh/id_rsa.pub` 命令查看生成的公钥,并完整的复制下来。 在github仓库界面点击自己的头像,选择`settings`。进入到设置页面后,点击左侧的`SSH and GPG keys`选项。点击`New SSH key`选项,并将复制下来的内容粘贴上去,添加该ssh key的描述。随后点击`Add SSH key`,并一路点击确认即可。
3. **本地安装rust**。进入linux环境下,参考Arceos 教程 [Rust 开发环境配置 - ArceOS Tutorial Book (rcore-os.cn)](https://rcore-os.cn/arceos-tutorial-book/ch01-02.html) 中,找到Rust 开发环境配置的章节,相应配置即可,你可以同时将后续需要的环境也配置好.
4. **clone实验仓库到本地。**在前面点击链接生成的仓库中,同样点击醒目的 `code` 绿色按钮,选择`local`下的`ssh`选项,复制下面的链接。随后回到本地linux环境下,使用`git clone 复制的链接`的方式,将目标仓库clone到本地。随后,使用`ls`命令查看自己clone下来的文件夹,再使用`cd`命令进入到该文件夹下,使用 `cargo install --force --path .` 安装rustlings。
5. **练习rustlings**。使用vscode等编辑器,进入clone下来的目录下的`exercises`文件夹,执行`rustlings watch`依次查看完成情况,并依次完成对应的练习。 执行`rustlings run 练习名称`去运行对应练习,也可以使用`rustlings hint 练习名称`查看题解。
6. **提交完成情况**。当做完部分或所有练习之后,在rustlings目录下执行 `git add .; git commit -m "update"; git push` 命令,把更新提交到GithubClassroom的CI进行自动评测。你可以在github仓库页面的actions页面,看到你的CI提交结果,或者 https://opencamp.ai/Rust/camp/S01/stage/1?tab=rank上面查看自己的评分
6. **提交完成情况**。当做完部分或所有练习之后,在rustlings目录下执行 `git add .; git commit -m "update"; git push` 命令,把更新提交到GithubClassroom的CI进行自动评测。你可以在github仓库页面的actions页面,看到你的CI提交结果,或者 https://opencamp.ai/Rust/camp/S01/stage/0?tab=rank 上面查看自己的评分
* 在线环境:

1. 如果使用在线环境,在本网页的中上部可以看到一个醒目的 `code` 绿色按钮,点击后,可以进一步看到 `codespace` 标签和醒目的 `create codesapce on main` 绿色按钮。请点击这个绿色按钮,就可以进入到在线的ubuntu +vscode环境中
Expand Down
2 changes: 1 addition & 1 deletion exercises/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Exercise to Book Chapter mapping
# Exercise to Book Chapter mappingd

| Exercise | Book Chapter |
| ---------------------- | ------------------- |
Expand Down
8 changes: 8 additions & 0 deletions exercises/functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Functions

Here, you'll learn how to write functions and how the Rust compiler can help you debug errors even
in more complex code.

## Further information

- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)
11 changes: 11 additions & 0 deletions exercises/functions/functions1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// functions1.rs
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a hint.



fn call_me(){
println!("Func1:hello world")
}
fn main() {
call_me();
}
14 changes: 14 additions & 0 deletions exercises/functions/functions2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// functions2.rs
// Execute `rustlings hint functions2` or use the `hint` watch subcommand for a hint.



fn main() {
call_me(3);
}

fn call_me(num:i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
14 changes: 14 additions & 0 deletions exercises/functions/functions3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// functions3.rs
// Execute `rustlings hint functions3` or use the `hint` watch subcommand for a hint.



fn main() {
call_me(1);
}

fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}
27 changes: 27 additions & 0 deletions exercises/functions/functions4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// functions4.rs
// Execute `rustlings hint functions4` or use the `hint` watch subcommand for a hint.

// This store is having a sale where if the price is an even number, you get
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off.
// (Don't worry about the function bodies themselves, we're only interested
// in the signatures for now. If anything, this is a good way to peek ahead
// to future exercises!)



fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> i32 {
if is_even(price) {
price - 10
} else {
price - 3
}
}

fn is_even(num: i32) -> bool {
num % 2 == 0
}
13 changes: 13 additions & 0 deletions exercises/functions/functions5.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// functions5.rs
// Execute `rustlings hint functions5` or use the `hint` watch subcommand for a hint.



fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
num * num
}
7 changes: 7 additions & 0 deletions exercises/if/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# If

`if`, the most basic (but still surprisingly versatile!) type of control flow, is what you'll learn here.

## Further information

- [Control Flow - if expressions](https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions)
31 changes: 31 additions & 0 deletions exercises/if/if1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// if1.rs
// Execute `rustlings hint if1` or use the `hint` watch subcommand for a hint.


pub fn bigger(a: i32, b: i32) -> i32 {
// Complete this function to return the bigger number!
// Do not use:
// - another function call
// - additional variables
if a>b{
a
}else{
b
}
}

// Don't mind this for now :)
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn ten_is_bigger_than_eight() {
assert_eq!(10, bigger(10, 8));
}

#[test]
fn fortytwo_is_bigger_than_thirtytwo() {
assert_eq!(42, bigger(32, 42));
}
}
38 changes: 38 additions & 0 deletions exercises/if/if2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// if2.rs

// Step 1: Make me compile!
// Step 2: Get the bar_for_fuzz and default_to_baz tests passing!
// Execute `rustlings hint if2` or use the `hint` watch subcommand for a hint.



pub fn foo_if_fizz(fizzish: &str) -> &str {
if fizzish == "fizz" {
"foo"
} else if fizzish == "fuzz" {
"bar"
}else{
"baz"
}
}

// No test changes needed!
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn foo_for_fizz() {
assert_eq!(foo_if_fizz("fizz"), "foo")
}

#[test]
fn bar_for_fuzz() {
assert_eq!(foo_if_fizz("fuzz"), "bar")
}

#[test]
fn default_to_baz() {
assert_eq!(foo_if_fizz("literally anything"), "baz")
}
}
55 changes: 55 additions & 0 deletions exercises/if/if3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// if3.rs
//
// Execute `rustlings hint if3` or use the `hint` watch subcommand for a hint.



pub fn animal_habitat(animal: &str) -> &'static str {
let identifier = if animal == "crab" {
1
} else if animal == "gopher" {
2
} else if animal == "snake" {
3
} else {
4
};

// DO NOT CHANGE THIS STATEMENT BELOW
let habitat = if identifier == 1 {
"Beach"
} else if identifier == 2 {
"Burrow"
} else if identifier == 3 {
"Desert"
} else {
"Unknown"
};

habitat
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn gopher_lives_in_burrow() {
assert_eq!(animal_habitat("gopher"), "Burrow")
}

#[test]
fn snake_lives_in_desert() {
assert_eq!(animal_habitat("snake"), "Desert")
}

#[test]
fn crab_lives_on_beach() {
assert_eq!(animal_habitat("crab"), "Beach")
}

#[test]
fn unknown_animal() {
assert_eq!(animal_habitat("dinosaur"), "Unknown")
}
}
8 changes: 8 additions & 0 deletions exercises/intro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Intro

Rust uses the `print!` and `println!` macros to print text to the console.

## Further information

- [Hello World](https://doc.rust-lang.org/rust-by-example/hello.html)
- [Formatted print](https://doc.rust-lang.org/rust-by-example/hello/print.html)
30 changes: 30 additions & 0 deletions exercises/intro/intro1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// intro1.rs
// About this `I AM NOT DONE` thing:
// We sometimes encourage you to keep trying things on a given exercise, even
// after you already figured it out. If you got everything working and feel
// ready for the next exercise, remove the `I AM NOT DONE` comment below.
// Execute `rustlings hint intro1` or use the `hint` watch subcommand for a hint.
//
// If you're running this using `rustlings watch`: The exercise file will be reloaded
// when you change one of the lines below! Try adding a `println!` line, or try changing
// what it outputs in your terminal. Try removing a semicolon and see what happens!



fn main() {
println!("Hello adajo!");
println!(r#" welcome to... "#);
println!(r#" _ _ _ "#);
println!(r#" _ __ _ _ ___| |_| (_)_ __ __ _ ___ "#);
println!(r#" | '__| | | / __| __| | | '_ \ / _` / __| "#);
println!(r#" | | | |_| \__ \ |_| | | | | | (_| \__ \ "#);
println!(r#" |_| \__,_|___/\__|_|_|_| |_|\__, |___/ "#);
println!(r#" |___/ "#);
println!();
println!("This exercise compiles successfully. The remaining exercises contain a compiler");
println!("or logic error. The central concept behind Rustlings is to fix these errors and");
println!("solve the exercises. Good luck!");
println!();
println!("The source for this exercise is in `exercises/intro/intro1.rs`. Have a look!");
println!("Going forward, the source of the exercises will always be in the success/failure output.");
}
9 changes: 9 additions & 0 deletions exercises/intro/intro2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// intro2.rs
// Make the code print a greeting to the world.
// Execute `rustlings hint intro2` or use the `hint` watch subcommand for a hint.



fn main() {
println!("Hello {}!","dahsdioahsdoafb");
}
32 changes: 32 additions & 0 deletions exercises/quiz1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// quiz1.rs
// This is a quiz for the following sections:
// - Variables
// - Functions
// - If

// Mary is buying apples. The price of an apple is calculated as follows:
// - An apple costs 2 rustbucks.
// - If Mary buys more than 40 apples, each apple only costs 1 rustbuck!
// Write a function that calculates the price of an order of apples given
// the quantity bought. No hints this time!



// Put your function here!
fn calculate_price_of_apples(num:u32)->u32 {
if num<=40 { num*2 } else { num }
}

// Don't modify this function!
#[test]
fn verify_test() {
let price1 = calculate_price_of_apples(35);
let price2 = calculate_price_of_apples(40);
let price3 = calculate_price_of_apples(41);
let price4 = calculate_price_of_apples(65);

assert_eq!(70, price1);
assert_eq!(80, price2);
assert_eq!(41, price3);
assert_eq!(65, price4);
}
9 changes: 9 additions & 0 deletions exercises/variables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Variables

In Rust, variables are immutable by default.
When a variable is immutable, once a value is bound to a name, you can’t change that value.
You can make them mutable by adding mut in front of the variable name.

## Further information

- [Variables and Mutability](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html)
9 changes: 9 additions & 0 deletions exercises/variables/variables1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// variables1.rs
// Make me compile!
// Execute `rustlings hint variables1` or use the `hint` watch subcommand for a hint.


fn main() {
let x = 5;
println!("x has the value {}", x);
}
13 changes: 13 additions & 0 deletions exercises/variables/variables2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// variables2.rs
// Execute `rustlings hint variables2` or use the `hint` watch subcommand for a hint.



fn main() {
let x:i32=42;
if x == 10 {
println!("x is ten!");
} else {
println!("x is not ten!");
}
}
9 changes: 9 additions & 0 deletions exercises/variables/variables3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// variables3.rs
// Execute `rustlings hint variables3` or use the `hint` watch subcommand for a hint.



fn main() {
let x: i32=10;
println!("Number {}", x);
}
11 changes: 11 additions & 0 deletions exercises/variables/variables4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// variables4.rs
// Execute `rustlings hint variables4` or use the `hint` watch subcommand for a hint.



fn main() {
let mut x = 3;
println!("Number {}", x);
x = 5; // don't change this line
println!("Number {}", x);
}
Loading