mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-03 07:48:41 +08:00
commit
086d7b473a
@ -361,7 +361,7 @@ executable application, as opposed to a library. Executables are often called
|
|||||||
*binaries* (as in `/usr/bin`, if you’re on a Unix system).
|
*binaries* (as in `/usr/bin`, if you’re on a Unix system).
|
||||||
|
|
||||||
Cargo has generated two files and one directory for us: a `Cargo.toml` and a
|
Cargo has generated two files and one directory for us: a `Cargo.toml` and a
|
||||||
*src* directory with a *main.rs* file inside. These should look familliar,
|
*src* directory with a *main.rs* file inside. These should look familiar,
|
||||||
they’re exactly what we created by hand, above.
|
they’re exactly what we created by hand, above.
|
||||||
|
|
||||||
This output is all you need to get started. First, open `Cargo.toml`. It should
|
This output is all you need to get started. First, open `Cargo.toml`. It should
|
||||||
|
@ -89,7 +89,7 @@ two possible values:
|
|||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
let t = true;
|
let t = true;
|
||||||
let f: bool = false; // with explict type annotation
|
let f: bool = false; // with explicit type annotation
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -110,7 +110,7 @@ fn main() {
|
|||||||
|
|
||||||
Rust’s `char` represents a [Unicode Scalar Value], which means that it can
|
Rust’s `char` represents a [Unicode Scalar Value], which means that it can
|
||||||
represent a lot more than just ASCII. ‘Character’ isn’t really a concept in
|
represent a lot more than just ASCII. ‘Character’ isn’t really a concept in
|
||||||
Unicode, however: your human intutition for what a ‘character’ is may not match
|
Unicode, however: your human intuition for what a ‘character’ is may not match
|
||||||
up with a `char`. It also means that `char`s are four bytes each.
|
up with a `char`. It also means that `char`s are four bytes each.
|
||||||
|
|
||||||
[Unicode Scalar Value]: http://www.unicode.org/glossary/#unicode_scalar_value
|
[Unicode Scalar Value]: http://www.unicode.org/glossary/#unicode_scalar_value
|
||||||
|
@ -55,7 +55,7 @@ As you can see, the first index is `0`.
|
|||||||
### Single-element tuples
|
### Single-element tuples
|
||||||
|
|
||||||
There’s one last trick with tuples: `(5)` is actually ambiguous: is it a tuple,
|
There’s one last trick with tuples: `(5)` is actually ambiguous: is it a tuple,
|
||||||
or is it a `5` in parethesis? If you need to disambiguate, use a comma:
|
or is it a `5` in parenthesis? If you need to disambiguate, use a comma:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -131,10 +131,10 @@ to the operating system: when it goes out of scope! When a variable goes out of
|
|||||||
scope, a special function is called. This function is called `drop()`, and it
|
scope, a special function is called. This function is called `drop()`, and it
|
||||||
is where the author of `String` can put the code to return the memory.
|
is where the author of `String` can put the code to return the memory.
|
||||||
|
|
||||||
> Aside: This pattern is sometimes called “Resource Aquisition Is
|
> Aside: This pattern is sometimes called “Resource Acquisition Is
|
||||||
> Initialization” in C++, or “RAII” for short. While they are very similar,
|
> Initialization” in C++, or “RAII” for short. While they are very similar,
|
||||||
> Rust’s take on this concept has a number of differences, and so we don’t tend
|
> Rust’s take on this concept has a number of differences, and so we don’t tend
|
||||||
> to use the same term. If you’re familliar with this idea, keep in mind that it
|
> to use the same term. If you’re familiar with this idea, keep in mind that it
|
||||||
> is _roughly_ similar in Rust, but not identical.
|
> is _roughly_ similar in Rust, but not identical.
|
||||||
|
|
||||||
This pattern has a profound impact on the way that Rust code is written. It may
|
This pattern has a profound impact on the way that Rust code is written. It may
|
||||||
@ -404,7 +404,7 @@ fn takes_and_gives_back(a_string: String) -> String {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
With simililar annotations:
|
With similiar annotations:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -180,7 +180,7 @@ let mut s = String::from("hello");
|
|||||||
let r2 = &mut s;
|
let r2 = &mut s;
|
||||||
```
|
```
|
||||||
|
|
||||||
There is a simlar rule for combining the two kinds of references. This code errors:
|
There is a similar rule for combining the two kinds of references. This code errors:
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
let mut s = String::from("hello");
|
let mut s = String::from("hello");
|
||||||
|
@ -110,7 +110,7 @@ Now we’re tracking both a start _and_ and ending index. Even more chances for
|
|||||||
things to go wrong. We now have three unrelated variable bindings floating
|
things to go wrong. We now have three unrelated variable bindings floating
|
||||||
around which need to be kept in sync.
|
around which need to be kept in sync.
|
||||||
|
|
||||||
Luckily, Rust has a solution to this probem: string slices.
|
Luckily, Rust has a solution to this problem: string slices.
|
||||||
|
|
||||||
# String slices
|
# String slices
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ let s2 = s1.clone();
|
|||||||
println!("{}", s1);
|
println!("{}", s1);
|
||||||
```
|
```
|
||||||
|
|
||||||
The call to `clone()` is attatched to `s1` with a dot. This is called ‘method
|
The call to `clone()` is attached to `s1` with a dot. This is called ‘method
|
||||||
syntax’, and it’s a way to call certain functions with a different style.
|
syntax’, and it’s a way to call certain functions with a different style.
|
||||||
|
|
||||||
Why have two ways to call functions? We’ll talk about some deeper reasons
|
Why have two ways to call functions? We’ll talk about some deeper reasons
|
||||||
@ -127,7 +127,7 @@ fn distance(p1: Point, p2: Point) -> f64 {
|
|||||||
# }
|
# }
|
||||||
```
|
```
|
||||||
|
|
||||||
Other than this, the rest of the example is familliar: an implementation of
|
Other than this, the rest of the example is familiar: an implementation of
|
||||||
`distance()`, and using the method to find an answer.
|
`distance()`, and using the method to find an answer.
|
||||||
|
|
||||||
There are two differences. The first is in the first argument. Instead of a name
|
There are two differences. The first is in the first argument. Instead of a name
|
||||||
|
@ -176,7 +176,7 @@ println!("x is: {}", x);
|
|||||||
```
|
```
|
||||||
|
|
||||||
We can't print out `x`! The error messages reference something we talked about
|
We can't print out `x`! The error messages reference something we talked about
|
||||||
breifly before, the `Display` trait. In order to implement this function, we
|
briefly before, the `Display` trait. In order to implement this function, we
|
||||||
need to talk about traits. But we only need to talk about traits to implement
|
need to talk about traits. But we only need to talk about traits to implement
|
||||||
our own generic functions; we don't need this understanding to use them. So
|
our own generic functions; we don't need this understanding to use them. So
|
||||||
rather than get into more details about this right now, let's talk about other
|
rather than get into more details about this right now, let's talk about other
|
||||||
|
@ -158,7 +158,7 @@ match some_u8_value {
|
|||||||
|
|
||||||
The `_` pattern matches anything at all, and so with it as the final pattern,
|
The `_` pattern matches anything at all, and so with it as the final pattern,
|
||||||
Rust can understand that we have all our bases covered. It's not only used for
|
Rust can understand that we have all our bases covered. It's not only used for
|
||||||
this sort of exhastiveness issue, though. It's useful any time we don't want to
|
this sort of exhaustiveness issue, though. It's useful any time we don't want to
|
||||||
deal with a number of cases. Consider this scenario: if we wanted to print out
|
deal with a number of cases. Consider this scenario: if we wanted to print out
|
||||||
something one one, three, five, and seven:
|
something one one, three, five, and seven:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user