update: translate variables

pull/110/head
unknown 3 years ago
parent 718c89ea91
commit b5216178fd

@ -1,9 +1,9 @@
# Variables # 变量(Variables
In Rust, variables are immutable by default. 在 Rust变量默认是不可变的.
When a variable is immutable, once a value is bound to a name, you cant change that value. 不可变意味着当一个值被绑定到某个名字上,你就不能再对这个值做出更改。
You can make them mutable by adding mut in front of the variable name. 当然,你可以通过在变量名前添加 mut 来使它们变得可变。
## Further information ## 更多信息
- [Variables and Mutability](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html) - [Variables and Mutability](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html)

@ -12,5 +12,5 @@
/// 加油 💪 /// 加油 💪
fn main() { fn main() {
x = 5; x = 5;
println!("x has the value {}", x); println!("x has the value {}", x);// 译x 的值是
} }

@ -1,13 +1,14 @@
// variables2.rs // variables2.rs
// Make me compile! Execute the command `rustlings hint variables2` if you want a hint :) // 让我能够编译!执行 `rustex hint variables2` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
/// 翻译: [mg-chao](https://github.com/mg-chao)
fn main() { fn main() {
let x; let x;
if x == 10 { if x == 10 {
println!("Ten!"); println!("Ten!");// 译:十!
} else { } else {
println!("Not ten!"); println!("Not ten!");// 译:不是十!
} }
} }

@ -1,11 +1,11 @@
// variables3.rs // variables3.rs
// Make me compile! Execute the command `rustlings hint variables3` if you want a hint :) // 让我能够编译!执行 `rustex hint variables3` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let x = 3; let x = 3;
println!("Number {}", x); println!("Number {}", x);// 译:"数字 {}"
x = 5; // don't change this line x = 5; // don't change this line(译:不要更改这一行)
println!("Number {}", x); println!("Number {}", x);
} }

@ -1,5 +1,5 @@
// variables4.rs // variables4.rs
// Make me compile! Execute the command `rustlings hint variables4` if you want a hint :) // 让我能够编译!执行 `rustex hint variables4` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE

@ -1,11 +1,11 @@
// variables5.rs // variables5.rs
// Make me compile! Execute the command `rustlings hint variables5` if you want a hint :) // 让我能够编译!执行 `rustex hint variables5` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE
fn main() { fn main() {
let number = "T-H-R-E-E"; // don't change this line let number = "T-H-R-E-E"; // don't change this line
println!("Spell a Number : {}", number); println!("Spell a Number : {}", number);// 译:"拼接的数字:{}"
number = 3; number = 3;
println!("Number plus two is : {}", number + 2); println!("Number plus two is : {}", number + 2);// 译:"数字加上二是:{}"
} }

@ -1,5 +1,5 @@
// variables6.rs // variables6.rs
// Make me compile! Execute the command `rustlings hint variables6` if you want a hint :) // 让我能够编译!执行 `rustex hint variables6` 获取提示 :)
// I AM NOT DONE // I AM NOT DONE

@ -13,62 +13,57 @@ name = "variables2"
path = "exercises/variables/variables2.rs" path = "exercises/variables/variables2.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
The compiler message is saying that Rust cannot infer the type that the Rust `x` .
variable binding `x` has with what is given here. 7
What happens if you annotate line 7 with a type annotation? x
What if you give x a value?
What if you do both? x
What type should x be, anyway? x 10 """
What if x is the same type as 10? What if it's a different type?"""
[[exercises]] [[exercises]]
name = "variables3" name = "variables3"
path = "exercises/variables/variables3.rs" path = "exercises/variables/variables3.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
In Rust, variable bindings are immutable by default. But here we're trying Rust
to reassign a different value to x! There's a keyword we can use to make x 使使"""
a variable binding mutable instead."""
[[exercises]] [[exercises]]
name = "variables4" name = "variables4"
path = "exercises/variables/variables4.rs" path = "exercises/variables/variables4.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
Oops! In this exercise, we have a variable binding that we've created on 7 8
line 7, and we're trying to use it on line 8, but we haven't given it a 使 x
value. We can't print out something that isn't there; try giving x a value! Bug Rust """
This is an error that can cause bugs that's very easy to make in any
programming language -- thankfully the Rust compiler has caught this for us!"""
[[exercises]] [[exercises]]
name = "variables5" name = "variables5"
path = "exercises/variables/variables5.rs" path = "exercises/variables/variables5.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
In variables3 we already learned how to make an immutable variable mutable variables3 使使
using a special keyword. Unfortunately this doesn't help us much in this exercise
because we want to assign a different typed value to an existing variable. Sometimes 使
you may also like to reuse existing variable names because you are just converting Rust Shadowing
values to different types like in this exercise. 'Variables and Mutability'* :
Fortunately Rust has a powerful solution to this problem: 'Shadowing'!
You can read more about 'Shadowing' in the book's section 'Variables and Mutability':
https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#shadowing
Try to solve this exercise afterwards using this technique.""" 使
Variables and Mutability"""
[[exercises]] [[exercises]]
name = "variables6" name = "variables6"
path = "exercises/variables/variables6.rs" path = "exercises/variables/variables6.rs"
mode = "compile" mode = "compile"
hint = """ hint = """
We know about variables and mutability, but there is another important type of Constant
variable available; constants. 'const' 'let'
Constants are always immutable and they are declared with keyword 'const' rather
than keyword 'let'.
Constants types must also always be annotated.
Read more about constants under 'Differences Between Variables and Constants' in the book's section 'Variables and Mutability': 'Differences Between Variables and Constants'* 'Variables and Mutability':
https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants
Differences Between Variables and Constants
""" """
# FUNCTIONS # FUNCTIONS

Loading…
Cancel
Save