Merge pull request #116 from mg-chao/20220102_functions

[rust-exercise] 翻译 functions 部分
pull/126/head
Sunface 3 years ago committed by GitHub
commit 53cce3d999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,7 +1,9 @@
# Functions # 函数(Functions
Here, you'll learn how to write functions and how Rust's compiler can trace things way back. 在本练习,你将学习如何编写一个函数,以及 Rust 编译器怎样可以对事物进行追溯trace things way back
## Further information 译:依据练习的内容,追溯的意思可能是类型推导之类的事。
## 更多信息
- [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html) - [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html)

@ -1,8 +1,9 @@
// functions1.rs // functions1.rs
// Make me compile! Execute `rustlings hint functions1` for hints :) // 让我能够编译!执行 `rustex hint functions1` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
call_me(); call_me();
} }

@ -1,14 +1,15 @@
// functions2.rs // functions2.rs
// Make me compile! Execute `rustlings hint functions2` for hints :) // 让我能够编译!执行 `rustex hint functions2` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
call_me(3); call_me(3);
} }
fn call_me(num:) { fn call_me(num:) {
for i in 0..num { for i in 0..num {
println!("Ring! Call number {}", i + 1); println!("Ring! Call number {}", i + 1);// 译:"叮!呼叫号码 {}"
} }
} }

@ -1,8 +1,9 @@
// functions3.rs // functions3.rs
// Make me compile! Execute `rustlings hint functions3` for hints :) // 让我能够编译!执行 `rustex hint functions3` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
call_me(); call_me();
} }

@ -1,14 +1,15 @@
// functions4.rs // functions4.rs
// Make me compile! Execute `rustlings hint functions4` for hints :) // 让我能够编译!执行 `rustex hint functions4` 获取提示 :)
// This store is having a sale where if the price is an even number, you get // 商店正在进行促销,如果价格是偶数,可以优惠 10 Rustbucks如果是奇数则优惠 3 Rustbucks。
// 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off. // Rustbucks 可能想表达 Rust元 的意思,好比 美元 。
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
let original_price = 51; let original_price = 51;
println!("Your sale price is {}", sale_price(original_price)); println!("Your sale price is {}", sale_price(original_price));// 译:"你需支付 {}"
} }
fn sale_price(price: i32) -> { fn sale_price(price: i32) -> {

@ -1,11 +1,12 @@
// functions5.rs // functions5.rs
// Make me compile! Execute `rustlings hint functions5` for hints :) // 让我能够编译!执行 `rustex hint functions5` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
let answer = square(3); let answer = square(3);
println!("The answer is {}", answer); println!("The answer is {}", answer);// 译:"答案是 {}"
} }
fn square(num: i32) -> i32 { fn square(num: i32) -> i32 {

@ -73,48 +73,44 @@ name = "functions1"
path = "exercises/functions/functions1.rs" path = "exercises/functions/functions1.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
This main function is calling a function that it expects to exist, but the `call_me`
function doesn't exist. It expects this function to have the name `call_me`.
It expects this function to not take any arguments and not return a value. `main` """
Sounds a lot like `main`, doesn't it?"""
[[exercises]] [[exercises]]
name = "functions2" name = "functions2"
path = "exercises/functions/functions2.rs" path = "exercises/functions/functions2.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Rust requires that all parts of a function's signature have type annotations, Rust signature `call_me` `num` """
but `call_me` is missing the type annotation of `num`."""
[[exercises]] [[exercises]]
name = "functions3" name = "functions3"
path = "exercises/functions/functions3.rs" path = "exercises/functions/functions3.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
This time, the function *declaration* is okay, but there's something wrong , *declaration* """
with the place where we're calling the function."""
[[exercises]] [[exercises]]
name = "functions4" name = "functions4"
path = "exercises/functions/functions4.rs" path = "exercises/functions/functions4.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
The error message points to line 14 and says it expects a type after the 15 `->`
`->`. This is where the function's return type should be-- take a look at `is_even` """
the `is_even` function for an example!"""
[[exercises]] [[exercises]]
name = "functions5" name = "functions5"
path = "exercises/functions/functions5.rs" path = "exercises/functions/functions5.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
This is a really common error that can be fixed by removing one character.
It happens because Rust distinguishes between expressions and statements: expressions return Rust operand,
a value based on its operand, and statements simply return a () type which behaves just like `void` in C/C++ language. `()` C/C++ `void`
We want to return a value of `i32` type from the `square` function, but it is returning a `()` type... `square` `i32` `()` ...
They are not the same. There are two solutions:
1. Add a `return` ahead of `num * num;` 1. `num * num;` `return`
2. remove `;`, make it to be `num * num`""" 2. `;` `num * num`"""
# IF # IF

Loading…
Cancel
Save