Skip to content

Commit e32221a

Browse files
committed
Write the 'primitive types' section of TRPL
A brief introduction to each type, with pointers to the primitive pages for more info.
1 parent e57410c commit e32221a

File tree

5 files changed

+282
-171
lines changed

5 files changed

+282
-171
lines changed

src/doc/trpl/SUMMARY.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* [`Deref` coercions](deref-coercions.md)
1818
* [Syntax and Semantics](syntax-and-semantics.md)
1919
* [Variable Bindings](variable-bindings.md)
20-
* [Primitive Types](primitive-types.md)
2120
* [Functions](functions.md)
21+
* [Primitive Types](primitive-types.md)
2222
* [Comments](comments.md)
2323
* [Structs](structs.md)
2424
* [Mutability](mutability.md)
@@ -35,8 +35,6 @@
3535
* [Move semantics](move-semantics.md)
3636
* [Drop](drop.md)
3737
* [Vectors](vectors.md)
38-
* [Arrays](arrays.md)
39-
* [Slices](slices.md)
4038
* [Strings](strings.md)
4139
* [Traits](traits.md)
4240
* [Operators and Overloading](operators-and-overloading.md)
@@ -47,7 +45,6 @@
4745
* [Crates and Modules](crates-and-modules.md)
4846
* [`static`](static.md)
4947
* [`const`](const.md)
50-
* [Tuples](tuples.md)
5148
* [Tuple Structs](tuple-structs.md)
5249
* [Attributes](attributes.md)
5350
* [Conditional Compilation](conditional-compilation.md)

src/doc/trpl/arrays.md

-48
This file was deleted.

src/doc/trpl/primitive-types.md

+281-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,283 @@
11
% Primitive Types
22

3-
Coming Soon!
3+
The Rust language has a number of types that are considered ‘primitive’. This
4+
means that they’re built-in to the language. Rust is structured in such a way
5+
that the standard library also provides a number of useful types built on top
6+
of these ones, as well, but these are the most primitive.
7+
8+
9+
# Unit, or `()`
10+
11+
The most basic built-in type is `()`, pronounced ‘unit’. The unit type has a
12+
single valid value, which is also written `()`.
13+
14+
The unit type is used as the default return type of functions:
15+
16+
```rust
17+
fn foo() {
18+
# }
19+
```
20+
21+
and
22+
23+
```rust
24+
fn foo() -> () {
25+
# }
26+
```
27+
28+
Are equivalent. This is the most common case for `()`: the value returned when
29+
you don’t want to return anything. Another example is the value of `let`:
30+
31+
```rust
32+
let x = 5;
33+
```
34+
35+
The value of this line is `()`.
36+
37+
# Booleans
38+
39+
Rust has a built in boolean type, named `bool`. It has two values, `true` and `false`:
40+
41+
```rust
42+
let x = true;
43+
44+
let y: bool = false;
45+
```
46+
47+
A common use of booleans is in [`if` statements][if].
48+
49+
[if]: if.html
50+
51+
You can find more documentation for `bool`s [in the standard library
52+
documentation][bool].
53+
54+
[bool]: ../std/primitive.bool.html
55+
56+
# `char`
57+
58+
The `char` type represents a single Unicode scalar value. You can create `char`s
59+
with a single tick: (`'`)
60+
61+
```rust
62+
let x = 'x';
63+
let two_hearts = '💕';
64+
```
65+
66+
Unlike some other languages, this means that Rust’s `char` is not a single byte,
67+
but four.
68+
69+
You can find more documentation for `char`s [in the standard library
70+
documentation][char].
71+
72+
[char]: ../std/primitive.char.html
73+
74+
# Numeric types
75+
76+
Rust has a variety of numeric types in a few categories: signed and unsigned,
77+
fixed and variable, floating-point and integer.
78+
79+
These types consist of two parts: the category, and the size. For example,
80+
`u16` is an unsigned type with sixteen bits of size. More bits lets you have
81+
bigger numbers.
82+
83+
If a number literal has nothing to cause its type to be inferred, it defaults:
84+
85+
```rust
86+
let x = 42; // x has type i32
87+
88+
let y = 1.0; // y has type f64
89+
```
90+
91+
## Signed and Unsigned
92+
93+
Integer types come in two varieties: signed and unsigned. To understand the
94+
difference, let’s consider a number with four bits of size. A signed, four-bit
95+
number would let you store numbers from `-8` to `+7`. Signed numbers use
96+
‘two’s compliment representation’. An unsigned four bit number, since it does
97+
not need to store negatives, can store values from `0` to `+15`.
98+
99+
Unsigned types use a `u` for their category, and signed types use `i`. The `i`
100+
is for ‘integer’. So `u8` is an eight-bit unsigned number, and `i8` is an
101+
eight-bit signed number.
102+
103+
## Fixed size types
104+
105+
Fixed size types have a specific number of bits in their representation. Valid
106+
bit sizes are `8`, `16`, `32`, and `64`. So, `u32` is an unsigned, 32-bit integer,
107+
and `i64` is a signed, 64-bit integer.
108+
109+
## Variable sized types
110+
111+
Rust also provides types whose size depends on the size of a pointer of the
112+
underlying machine. These types have ‘size’ as the category, and come in signed
113+
and unsigned varieties. This makes for two types: `isize` and `usize`.
114+
115+
## Floating-point types
116+
117+
Rust also two floating point types: `f32` and `f64`. These correspond to
118+
IEEE-754 single and double precision numbers.
119+
120+
# Arrays
121+
122+
Like many programming languages, Rust has list types to represent a sequence of
123+
things. The most basic is the *array*, a fixed-size list of elements of the
124+
same type. By default, arrays are immutable.
125+
126+
```rust
127+
let a = [1, 2, 3]; // a: [i32; 3]
128+
let mut m = [1, 2, 3]; // mut m: [i32; 3]
129+
```
130+
131+
Arrays have type `[T; N]`. We’ll talk about this `T` notation [in the generics
132+
section][generics].
133+
134+
There’s a shorthand for initializing each element of an array to the same
135+
value. In this example, each element of `a` will be initialized to `0`:
136+
137+
```rust
138+
let a = [0; 20]; // a: [i32; 20]
139+
```
140+
141+
You can get the number of elements in an array `a` with `a.len()`:
142+
143+
```rust
144+
let a = [1, 2, 3];
145+
146+
println!("a has {} elements", a.len());
147+
```
148+
149+
You can access a particular element of an array with *subscript notation*:
150+
151+
```rust
152+
let names = ["Graydon", "Brian", "Niko"]; // names: [&str; 3]
153+
154+
println!("The second name is: {}", names[1]);
155+
```
156+
157+
Subscripts start at zero, like in most programming languages, so the first name
158+
is `names[0]` and the second name is `names[1]`. The above example prints
159+
`The second name is: Brian`. If you try to use a subscript that is not in the
160+
array, you will get an error: array access is bounds-checked at run-time. Such
161+
errant access is the source of many bugs in other systems programming
162+
languages.
163+
164+
You can find more documentation for `array`s [in the standard library
165+
documentation][array].
166+
167+
[array]: ../std/primitive.array.html
168+
169+
# Slices
170+
171+
A ‘slice’ is a reference to (or “view” into) an array. They are useful for
172+
allowing safe, efficient access to a portion of an array without copying. For
173+
example, you might want to reference just one line of a file read into memory.
174+
By nature, a slice is not created directly, but from an existing variable.
175+
Slices have a length, can be mutable or not, and in many ways behave like
176+
arrays:
177+
178+
```rust
179+
let a = [0, 1, 2, 3, 4];
180+
let middle = &a[1..4]; // A slice of a: just the elements 1, 2, and 3
181+
```
182+
183+
Slices have type `&[T]`. We’ll talk about that `T` when we cover
184+
[generics][generics].
185+
186+
[generics]: generics.html
187+
188+
You can find more documentation for `slices`s [in the standard library
189+
documentation][slice].
190+
191+
[slice]: ../std/primitive.slice.html
192+
193+
# `str`
194+
195+
Rust’s `str` type is the most primitive string type. As an [unsized type][dst],
196+
it’s not very useful by itself, but becomes useful when placed behind a reference,
197+
like [`&str`][strings]. As such, we’ll just leave it at that.
198+
199+
[dst]: unsized-types.html
200+
[strings]: strings.html
201+
202+
You can find more documentation for `str` [in the standard library
203+
documentation][str].
204+
205+
[str]: ../std/primitive.str.html
206+
207+
# Tuples
208+
209+
A tuple is an ordered list of fixed size. Like this:
210+
211+
```rust
212+
let x = (1, "hello");
213+
```
214+
215+
The parentheses and commas form this two-length tuple. Here’s the same code, but
216+
with the type annotated:
217+
218+
```rust
219+
let x: (i32, &str) = (1, "hello");
220+
```
221+
222+
As you can see, the type of a tuple looks just like the tuple, but with each
223+
position having a type name rather than the value. Careful readers will also
224+
note that tuples are heterogeneous: we have an `i32` and a `&str` in this tuple.
225+
In systems programming languages, strings are a bit more complex than in other
226+
languages. For now, just read `&str` as a *string slice*, and we’ll learn more
227+
soon.
228+
229+
You can access the fields in a tuple through a *destructuring let*. Here’s
230+
an example:
231+
232+
```rust
233+
let (x, y, z) = (1, 2, 3);
234+
235+
println!("x is {}", x);
236+
```
237+
238+
Remember [before][let] when I said the left-hand side of a `let` statement was more
239+
powerful than just assigning a binding? Here we are. We can put a pattern on
240+
the left-hand side of the `let`, and if it matches up to the right-hand side,
241+
we can assign multiple bindings at once. In this case, `let` "destructures,"
242+
or "breaks up," the tuple, and assigns the bits to three bindings.
243+
244+
[let]: variable-bindings.html
245+
246+
This pattern is very powerful, and we’ll see it repeated more later.
247+
248+
There are also a few things you can do with a tuple as a whole, without
249+
destructuring. You can assign one tuple into another, if they have the same
250+
contained types and [arity]. Tuples have the same arity when they have the same
251+
length.
252+
253+
[arity]: glossary.html#arity
254+
255+
```rust
256+
let mut x = (1, 2); // x: (i32, i32)
257+
let y = (2, 3); // y: (i32, i32)
258+
259+
x = y;
260+
```
261+
262+
You can find more documentation for tuples [in the standard library
263+
documentation][tuple].
264+
265+
[tuple]: ../std/primitive.tuple.html
266+
267+
# Functions
268+
269+
Functions also have a type! They look like this:
270+
271+
```
272+
fn foo(x: i32) -> i32 { x }
273+
274+
let x: fn(i32) -> i32 = foo;
275+
```
276+
277+
In this case, `x` is a ‘function pointer’ to a function that takes an `i32` and
278+
returns an `i32`.
279+
280+
You can find more documentation for function types [in the standard library
281+
documentation][fn].
282+
283+
[fn]: ../std/primitive.fn.html

src/doc/trpl/slices.md

-21
This file was deleted.

0 commit comments

Comments
 (0)