2022-12-21 16:36:30 +01:00
|
|
|
# Scalar Types
|
|
|
|
|
2023-07-18 07:12:51 +02:00
|
|
|
| | Types | Literals |
|
|
|
|
|------------------------|--------------------------------------------|--------------------------------|
|
|
|
|
| Signed integers | `i8`, `i16`, `i32`, `i64`, `i128`, `isize` | `-10`, `0`, `1_000`, `123_i64` |
|
|
|
|
| Unsigned integers | `u8`, `u16`, `u32`, `u64`, `u128`, `usize` | `0`, `123`, `10_u16` |
|
|
|
|
| Floating point numbers | `f32`, `f64` | `3.14`, `-10.0e20`, `2_f32` |
|
|
|
|
| Strings | `&str` | `"foo"`, `"two\nlines"` |
|
|
|
|
| Unicode scalar values | `char` | `'a'`, `'α'`, `'∞'` |
|
|
|
|
| Booleans | `bool` | `true`, `false` |
|
2022-12-21 16:36:30 +01:00
|
|
|
|
|
|
|
The types have widths as follows:
|
|
|
|
|
2022-12-26 19:35:09 -08:00
|
|
|
* `iN`, `uN`, and `fN` are _N_ bits wide,
|
2022-12-21 16:36:30 +01:00
|
|
|
* `isize` and `usize` are the width of a pointer,
|
2023-07-06 08:39:47 +00:00
|
|
|
* `char` is 32 bits wide,
|
|
|
|
* `bool` is 8 bits wide.
|
2023-04-27 10:57:07 -07:00
|
|
|
|
|
|
|
<details>
|
|
|
|
|
|
|
|
There are a few syntaxes which are not shown above:
|
|
|
|
|
|
|
|
- Raw strings allow you to create a `&str` value with escapes disabled: `r"\n"
|
|
|
|
== "\\\\n"`. You can embed double-quotes by using an equal amount of `#` on
|
|
|
|
either side of the quotes:
|
|
|
|
|
2023-09-26 17:04:46 +02:00
|
|
|
<!-- mdbook-xgettext: skip -->
|
2023-04-27 10:57:07 -07:00
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
println!(r#"<a href="link.html">link</a>"#);
|
|
|
|
println!("<a href=\"link.html\">link</a>");
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
- Byte strings allow you to create a `&[u8]` value directly:
|
|
|
|
|
2023-09-26 17:04:46 +02:00
|
|
|
<!-- mdbook-xgettext: skip -->
|
2023-04-27 10:57:07 -07:00
|
|
|
```rust,editable
|
|
|
|
fn main() {
|
|
|
|
println!("{:?}", b"abc");
|
|
|
|
println!("{:?}", &[97, 98, 99]);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-07-18 07:12:51 +02:00
|
|
|
- All underscores in numbers can be left out, they are for legibility only.
|
|
|
|
So `1_000` can be written as `1000` (or `10_00`), and `123_i64` can be written as `123i64`.
|
|
|
|
|
2023-04-27 10:57:07 -07:00
|
|
|
</details>
|