mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-24 16:10:25 +08:00
Merge pull request #209 from jturner314/byte-literals
Clarify that there isn't a separate "byte type"
This commit is contained in:
commit
8687b94bb8
@ -59,6 +59,20 @@ Finally, the `isize` and `usize` types depend on the kind of computer your
|
||||
program is running on: 64-bits if you're on a 64-bit architecture, and 32-bits
|
||||
if you’re on a 32-bit architecture.
|
||||
|
||||
You can write integer literals in any of the forms shown in Table 3-2. Note that
|
||||
all number literals except for the byte literal allow a type suffix, such as
|
||||
`57u8`, and `_` as a visual separator, such as `1_000`.
|
||||
|
||||
| Number literals | Example |
|
||||
|------------------|---------------|
|
||||
| Decimal | `98_222` |
|
||||
| Hex | `0xff` |
|
||||
| Octal | `0o77` |
|
||||
| Binary | `0b1111_0000` |
|
||||
| Byte (`u8` only) | `b'A'` |
|
||||
|
||||
*Table 3-2: Integer literals in Rust.*
|
||||
|
||||
So how do you know which type of integer to use? If you're unsure, Rust's
|
||||
defaults are generally good choices, and integer types default to `i32`: it’s
|
||||
generally the fastest, even on 64-bit systems. The primary situation in which
|
||||
@ -168,25 +182,6 @@ about Unicode Scalar Values at
|
||||
*http://www.unicode.org/glossary/#unicode_scalar_value* and find a chart for
|
||||
all unicode code points at *http://www.unicode.org/charts/*.
|
||||
|
||||
#### The Byte Type
|
||||
|
||||
You can work with the bytes of data directly. Byte literals can be created from
|
||||
the ASCII characters using `b` and single quotes:
|
||||
|
||||
Filename: src/main.rs
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let byte = b'a';
|
||||
println!("byte is {}", byte);
|
||||
}
|
||||
```
|
||||
|
||||
This will print `byte is 97`. Similarly, byte string literals can be created
|
||||
using `b` and double quotes, like `b"some byte string"`. Note that since you are
|
||||
limited to ASCII characters, it's a best practice to use characters instead of
|
||||
bytes when you're working with natural language text.
|
||||
|
||||
### Compound Types
|
||||
|
||||
*Compound types* can group multiple values of other types into one type. Rust
|
||||
|
Loading…
Reference in New Issue
Block a user