]> git.proxmox.com Git - rustc.git/blob - src/libstd/primitive_docs.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libstd / primitive_docs.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #[doc(primitive = "bool")]
12 //
13 /// The boolean type.
14 ///
15 /// The `bool` represents a value, which could only be either `true` or `false`. If you cast
16 /// a `bool` into an integer, `true` will be 1 and `false` will be 0.
17 ///
18 /// # Basic usage
19 ///
20 /// `bool` implements various traits, such as [`BitAnd`], [`BitOr`], [`Not`], etc.,
21 /// which allow us to perform boolean operations using `&`, `|` and `!`.
22 ///
23 /// [`if`] always demands a `bool` value. [`assert!`], being an important macro in testing,
24 /// checks whether an expression returns `true`.
25 ///
26 /// ```
27 /// let bool_val = true & false | false;
28 /// assert!(!bool_val);
29 /// ```
30 ///
31 /// [`assert!`]: macro.assert.html
32 /// [`if`]: ../book/first-edition/if.html
33 /// [`BitAnd`]: ops/trait.BitAnd.html
34 /// [`BitOr`]: ops/trait.BitOr.html
35 /// [`Not`]: ops/trait.Not.html
36 ///
37 /// # Examples
38 ///
39 /// A trivial example of the usage of `bool`,
40 ///
41 /// ```
42 /// let praise_the_borrow_checker = true;
43 ///
44 /// // using the `if` conditional
45 /// if praise_the_borrow_checker {
46 /// println!("oh, yeah!");
47 /// } else {
48 /// println!("what?!!");
49 /// }
50 ///
51 /// // ... or, a match pattern
52 /// match praise_the_borrow_checker {
53 /// true => println!("keep praising!"),
54 /// false => println!("you should praise!"),
55 /// }
56 /// ```
57 ///
58 /// Also, since `bool` implements the [`Copy`](marker/trait.Copy.html) trait, we don't
59 /// have to worry about the move semantics (just like the integer and float primitives).
60 ///
61 /// Now an example of `bool` cast to integer type:
62 ///
63 /// ```
64 /// assert_eq!(true as i32, 1);
65 /// assert_eq!(false as i32, 0);
66 /// ```
67 #[stable(feature = "rust1", since = "1.0.0")]
68 mod prim_bool { }
69
70 #[doc(primitive = "char")]
71 //
72 /// A character type.
73 ///
74 /// The `char` type represents a single character. More specifically, since
75 /// 'character' isn't a well-defined concept in Unicode, `char` is a '[Unicode
76 /// scalar value]', which is similar to, but not the same as, a '[Unicode code
77 /// point]'.
78 ///
79 /// [Unicode scalar value]: http://www.unicode.org/glossary/#unicode_scalar_value
80 /// [Unicode code point]: http://www.unicode.org/glossary/#code_point
81 ///
82 /// This documentation describes a number of methods and trait implementations on the
83 /// `char` type. For technical reasons, there is additional, separate
84 /// documentation in [the `std::char` module](char/index.html) as well.
85 ///
86 /// # Representation
87 ///
88 /// `char` is always four bytes in size. This is a different representation than
89 /// a given character would have as part of a [`String`]. For example:
90 ///
91 /// ```
92 /// let v = vec!['h', 'e', 'l', 'l', 'o'];
93 ///
94 /// // five elements times four bytes for each element
95 /// assert_eq!(20, v.len() * std::mem::size_of::<char>());
96 ///
97 /// let s = String::from("hello");
98 ///
99 /// // five elements times one byte per element
100 /// assert_eq!(5, s.len() * std::mem::size_of::<u8>());
101 /// ```
102 ///
103 /// [`String`]: string/struct.String.html
104 ///
105 /// As always, remember that a human intuition for 'character' may not map to
106 /// Unicode's definitions. For example, despite looking similar, the 'é'
107 /// character is one Unicode code point while 'é' is two Unicode code points:
108 ///
109 /// ```
110 /// let mut chars = "é".chars();
111 /// // U+00e9: 'latin small letter e with acute'
112 /// assert_eq!(Some('\u{00e9}'), chars.next());
113 /// assert_eq!(None, chars.next());
114 ///
115 /// let mut chars = "é".chars();
116 /// // U+0065: 'latin small letter e'
117 /// assert_eq!(Some('\u{0065}'), chars.next());
118 /// // U+0301: 'combining acute accent'
119 /// assert_eq!(Some('\u{0301}'), chars.next());
120 /// assert_eq!(None, chars.next());
121 /// ```
122 ///
123 /// This means that the contents of the first string above _will_ fit into a
124 /// `char` while the contents of the second string _will not_. Trying to create
125 /// a `char` literal with the contents of the second string gives an error:
126 ///
127 /// ```text
128 /// error: character literal may only contain one codepoint: 'é'
129 /// let c = 'é';
130 /// ^^^^
131 /// ```
132 ///
133 /// Another implication of the 4-byte fixed size of a `char` is that
134 /// per-`char` processing can end up using a lot more memory:
135 ///
136 /// ```
137 /// let s = String::from("love: ❤️");
138 /// let v: Vec<char> = s.chars().collect();
139 ///
140 /// assert_eq!(12, s.len() * std::mem::size_of::<u8>());
141 /// assert_eq!(32, v.len() * std::mem::size_of::<char>());
142 /// ```
143 #[stable(feature = "rust1", since = "1.0.0")]
144 mod prim_char { }
145
146 #[doc(primitive = "unit")]
147 //
148 /// The `()` type, sometimes called "unit" or "nil".
149 ///
150 /// The `()` type has exactly one value `()`, and is used when there
151 /// is no other meaningful value that could be returned. `()` is most
152 /// commonly seen implicitly: functions without a `-> ...` implicitly
153 /// have return type `()`, that is, these are equivalent:
154 ///
155 /// ```rust
156 /// fn long() -> () {}
157 ///
158 /// fn short() {}
159 /// ```
160 ///
161 /// The semicolon `;` can be used to discard the result of an
162 /// expression at the end of a block, making the expression (and thus
163 /// the block) evaluate to `()`. For example,
164 ///
165 /// ```rust
166 /// fn returns_i64() -> i64 {
167 /// 1i64
168 /// }
169 /// fn returns_unit() {
170 /// 1i64;
171 /// }
172 ///
173 /// let is_i64 = {
174 /// returns_i64()
175 /// };
176 /// let is_unit = {
177 /// returns_i64();
178 /// };
179 /// ```
180 ///
181 #[stable(feature = "rust1", since = "1.0.0")]
182 mod prim_unit { }
183
184 #[doc(primitive = "pointer")]
185 //
186 /// Raw, unsafe pointers, `*const T`, and `*mut T`.
187 ///
188 /// Working with raw pointers in Rust is uncommon,
189 /// typically limited to a few patterns.
190 ///
191 /// Use the [`null`] and [`null_mut`] functions to create null pointers, and the
192 /// [`is_null`] method of the `*const T` and `*mut T` types to check for null.
193 /// The `*const T` and `*mut T` types also define the [`offset`] method, for
194 /// pointer math.
195 ///
196 /// # Common ways to create raw pointers
197 ///
198 /// ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`).
199 ///
200 /// ```
201 /// let my_num: i32 = 10;
202 /// let my_num_ptr: *const i32 = &my_num;
203 /// let mut my_speed: i32 = 88;
204 /// let my_speed_ptr: *mut i32 = &mut my_speed;
205 /// ```
206 ///
207 /// To get a pointer to a boxed value, dereference the box:
208 ///
209 /// ```
210 /// let my_num: Box<i32> = Box::new(10);
211 /// let my_num_ptr: *const i32 = &*my_num;
212 /// let mut my_speed: Box<i32> = Box::new(88);
213 /// let my_speed_ptr: *mut i32 = &mut *my_speed;
214 /// ```
215 ///
216 /// This does not take ownership of the original allocation
217 /// and requires no resource management later,
218 /// but you must not use the pointer after its lifetime.
219 ///
220 /// ## 2. Consume a box (`Box<T>`).
221 ///
222 /// The [`into_raw`] function consumes a box and returns
223 /// the raw pointer. It doesn't destroy `T` or deallocate any memory.
224 ///
225 /// ```
226 /// let my_speed: Box<i32> = Box::new(88);
227 /// let my_speed: *mut i32 = Box::into_raw(my_speed);
228 ///
229 /// // By taking ownership of the original `Box<T>` though
230 /// // we are obligated to put it together later to be destroyed.
231 /// unsafe {
232 /// drop(Box::from_raw(my_speed));
233 /// }
234 /// ```
235 ///
236 /// Note that here the call to [`drop`] is for clarity - it indicates
237 /// that we are done with the given value and it should be destroyed.
238 ///
239 /// ## 3. Get it from C.
240 ///
241 /// ```
242 /// # #![feature(libc)]
243 /// extern crate libc;
244 ///
245 /// use std::mem;
246 ///
247 /// fn main() {
248 /// unsafe {
249 /// let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>()) as *mut i32;
250 /// if my_num.is_null() {
251 /// panic!("failed to allocate memory");
252 /// }
253 /// libc::free(my_num as *mut libc::c_void);
254 /// }
255 /// }
256 /// ```
257 ///
258 /// Usually you wouldn't literally use `malloc` and `free` from Rust,
259 /// but C APIs hand out a lot of pointers generally, so are a common source
260 /// of raw pointers in Rust.
261 ///
262 /// *[See also the `std::ptr` module](ptr/index.html).*
263 ///
264 /// [`null`]: ../std/ptr/fn.null.html
265 /// [`null_mut`]: ../std/ptr/fn.null_mut.html
266 /// [`is_null`]: ../std/primitive.pointer.html#method.is_null
267 /// [`offset`]: ../std/primitive.pointer.html#method.offset
268 /// [`into_raw`]: ../std/boxed/struct.Box.html#method.into_raw
269 /// [`drop`]: ../std/mem/fn.drop.html
270 #[stable(feature = "rust1", since = "1.0.0")]
271 mod prim_pointer { }
272
273 #[doc(primitive = "array")]
274 //
275 /// A fixed-size array, denoted `[T; N]`, for the element type, `T`, and the
276 /// non-negative compile-time constant size, `N`.
277 ///
278 /// There are two syntactic forms for creating an array:
279 ///
280 /// * A list with each element, i.e. `[x, y, z]`.
281 /// * A repeat expression `[x; N]`, which produces an array with `N` copies of `x`.
282 /// The type of `x` must be [`Copy`][copy].
283 ///
284 /// Arrays of sizes from 0 to 32 (inclusive) implement the following traits if
285 /// the element type allows it:
286 ///
287 /// - [`Debug`][debug]
288 /// - [`IntoIterator`][intoiterator] (implemented for `&[T; N]` and `&mut [T; N]`)
289 /// - [`PartialEq`][partialeq], [`PartialOrd`][partialord], [`Eq`][eq], [`Ord`][ord]
290 /// - [`Hash`][hash]
291 /// - [`AsRef`][asref], [`AsMut`][asmut]
292 /// - [`Borrow`][borrow], [`BorrowMut`][borrowmut]
293 /// - [`Default`][default]
294 ///
295 /// This limitation on the size `N` exists because Rust does not yet support
296 /// code that is generic over the size of an array type. `[Foo; 3]` and `[Bar; 3]`
297 /// are instances of same generic type `[T; 3]`, but `[Foo; 3]` and `[Foo; 5]` are
298 /// entirely different types. As a stopgap, trait implementations are
299 /// statically generated up to size 32.
300 ///
301 /// Arrays of *any* size are [`Copy`][copy] if the element type is [`Copy`][copy]
302 /// and [`Clone`][clone] if the element type is [`Clone`][clone]. This works
303 /// because [`Copy`][copy] and [`Clone`][clone] traits are specially known
304 /// to the compiler.
305 ///
306 /// Arrays coerce to [slices (`[T]`)][slice], so a slice method may be called on
307 /// an array. Indeed, this provides most of the API for working with arrays.
308 /// Slices have a dynamic size and do not coerce to arrays.
309 ///
310 /// There is no way to move elements out of an array. See [`mem::replace`][replace]
311 /// for an alternative.
312 ///
313 /// # Examples
314 ///
315 /// ```
316 /// let mut array: [i32; 3] = [0; 3];
317 ///
318 /// array[1] = 1;
319 /// array[2] = 2;
320 ///
321 /// assert_eq!([1, 2], &array[1..]);
322 ///
323 /// // This loop prints: 0 1 2
324 /// for x in &array {
325 /// print!("{} ", x);
326 /// }
327 /// ```
328 ///
329 /// An array itself is not iterable:
330 ///
331 /// ```compile_fail,E0277
332 /// let array: [i32; 3] = [0; 3];
333 ///
334 /// for x in array { }
335 /// // error: the trait bound `[i32; 3]: std::iter::Iterator` is not satisfied
336 /// ```
337 ///
338 /// The solution is to coerce the array to a slice by calling a slice method:
339 ///
340 /// ```
341 /// # let array: [i32; 3] = [0; 3];
342 /// for x in array.iter() { }
343 /// ```
344 ///
345 /// If the array has 32 or fewer elements (see above), you can also use the
346 /// array reference's [`IntoIterator`] implementation:
347 ///
348 /// ```
349 /// # let array: [i32; 3] = [0; 3];
350 /// for x in &array { }
351 /// ```
352 ///
353 /// [slice]: primitive.slice.html
354 /// [copy]: marker/trait.Copy.html
355 /// [clone]: clone/trait.Clone.html
356 /// [debug]: fmt/trait.Debug.html
357 /// [intoiterator]: iter/trait.IntoIterator.html
358 /// [partialeq]: cmp/trait.PartialEq.html
359 /// [partialord]: cmp/trait.PartialOrd.html
360 /// [eq]: cmp/trait.Eq.html
361 /// [ord]: cmp/trait.Ord.html
362 /// [hash]: hash/trait.Hash.html
363 /// [asref]: convert/trait.AsRef.html
364 /// [asmut]: convert/trait.AsMut.html
365 /// [borrow]: borrow/trait.Borrow.html
366 /// [borrowmut]: borrow/trait.BorrowMut.html
367 /// [default]: default/trait.Default.html
368 /// [replace]: mem/fn.replace.html
369 /// [`IntoIterator`]: iter/trait.IntoIterator.html
370 ///
371 #[stable(feature = "rust1", since = "1.0.0")]
372 mod prim_array { }
373
374 #[doc(primitive = "slice")]
375 //
376 /// A dynamically-sized view into a contiguous sequence, `[T]`.
377 ///
378 /// Slices are a view into a block of memory represented as a pointer and a
379 /// length.
380 ///
381 /// ```
382 /// // slicing a Vec
383 /// let vec = vec![1, 2, 3];
384 /// let int_slice = &vec[..];
385 /// // coercing an array to a slice
386 /// let str_slice: &[&str] = &["one", "two", "three"];
387 /// ```
388 ///
389 /// Slices are either mutable or shared. The shared slice type is `&[T]`,
390 /// while the mutable slice type is `&mut [T]`, where `T` represents the element
391 /// type. For example, you can mutate the block of memory that a mutable slice
392 /// points to:
393 ///
394 /// ```
395 /// let x = &mut [1, 2, 3];
396 /// x[1] = 7;
397 /// assert_eq!(x, &[1, 7, 3]);
398 /// ```
399 ///
400 /// *[See also the `std::slice` module](slice/index.html).*
401 ///
402 #[stable(feature = "rust1", since = "1.0.0")]
403 mod prim_slice { }
404
405 #[doc(primitive = "str")]
406 //
407 /// String slices.
408 ///
409 /// The `str` type, also called a 'string slice', is the most primitive string
410 /// type. It is usually seen in its borrowed form, `&str`. It is also the type
411 /// of string literals, `&'static str`.
412 ///
413 /// Strings slices are always valid UTF-8.
414 ///
415 /// This documentation describes a number of methods and trait implementations
416 /// on the `str` type. For technical reasons, there is additional, separate
417 /// documentation in the [`std::str`](str/index.html) module as well.
418 ///
419 /// # Examples
420 ///
421 /// String literals are string slices:
422 ///
423 /// ```
424 /// let hello = "Hello, world!";
425 ///
426 /// // with an explicit type annotation
427 /// let hello: &'static str = "Hello, world!";
428 /// ```
429 ///
430 /// They are `'static` because they're stored directly in the final binary, and
431 /// so will be valid for the `'static` duration.
432 ///
433 /// # Representation
434 ///
435 /// A `&str` is made up of two components: a pointer to some bytes, and a
436 /// length. You can look at these with the [`as_ptr`] and [`len`] methods:
437 ///
438 /// ```
439 /// use std::slice;
440 /// use std::str;
441 ///
442 /// let story = "Once upon a time...";
443 ///
444 /// let ptr = story.as_ptr();
445 /// let len = story.len();
446 ///
447 /// // story has nineteen bytes
448 /// assert_eq!(19, len);
449 ///
450 /// // We can re-build a str out of ptr and len. This is all unsafe because
451 /// // we are responsible for making sure the two components are valid:
452 /// let s = unsafe {
453 /// // First, we build a &[u8]...
454 /// let slice = slice::from_raw_parts(ptr, len);
455 ///
456 /// // ... and then convert that slice into a string slice
457 /// str::from_utf8(slice)
458 /// };
459 ///
460 /// assert_eq!(s, Ok(story));
461 /// ```
462 ///
463 /// [`as_ptr`]: #method.as_ptr
464 /// [`len`]: #method.len
465 ///
466 /// Note: This example shows the internals of `&str`. `unsafe` should not be
467 /// used to get a string slice under normal circumstances. Use `as_slice`
468 /// instead.
469 #[stable(feature = "rust1", since = "1.0.0")]
470 mod prim_str { }
471
472 #[doc(primitive = "tuple")]
473 //
474 /// A finite heterogeneous sequence, `(T, U, ..)`.
475 ///
476 /// Let's cover each of those in turn:
477 ///
478 /// Tuples are *finite*. In other words, a tuple has a length. Here's a tuple
479 /// of length `3`:
480 ///
481 /// ```
482 /// ("hello", 5, 'c');
483 /// ```
484 ///
485 /// 'Length' is also sometimes called 'arity' here; each tuple of a different
486 /// length is a different, distinct type.
487 ///
488 /// Tuples are *heterogeneous*. This means that each element of the tuple can
489 /// have a different type. In that tuple above, it has the type:
490 ///
491 /// ```
492 /// # let _:
493 /// (&'static str, i32, char)
494 /// # = ("hello", 5, 'c');
495 /// ```
496 ///
497 /// Tuples are a *sequence*. This means that they can be accessed by position;
498 /// this is called 'tuple indexing', and it looks like this:
499 ///
500 /// ```rust
501 /// let tuple = ("hello", 5, 'c');
502 ///
503 /// assert_eq!(tuple.0, "hello");
504 /// assert_eq!(tuple.1, 5);
505 /// assert_eq!(tuple.2, 'c');
506 /// ```
507 ///
508 /// For more about tuples, see [the book](../book/first-edition/primitive-types.html#tuples).
509 ///
510 /// # Trait implementations
511 ///
512 /// If every type inside a tuple implements one of the following traits, then a
513 /// tuple itself also implements it.
514 ///
515 /// * [`Clone`]
516 /// * [`Copy`]
517 /// * [`PartialEq`]
518 /// * [`Eq`]
519 /// * [`PartialOrd`]
520 /// * [`Ord`]
521 /// * [`Debug`]
522 /// * [`Default`]
523 /// * [`Hash`]
524 ///
525 /// [`Clone`]: clone/trait.Clone.html
526 /// [`Copy`]: marker/trait.Copy.html
527 /// [`PartialEq`]: cmp/trait.PartialEq.html
528 /// [`Eq`]: cmp/trait.Eq.html
529 /// [`PartialOrd`]: cmp/trait.PartialOrd.html
530 /// [`Ord`]: cmp/trait.Ord.html
531 /// [`Debug`]: fmt/trait.Debug.html
532 /// [`Default`]: default/trait.Default.html
533 /// [`Hash`]: hash/trait.Hash.html
534 ///
535 /// Due to a temporary restriction in Rust's type system, these traits are only
536 /// implemented on tuples of arity 12 or less. In the future, this may change.
537 ///
538 /// # Examples
539 ///
540 /// Basic usage:
541 ///
542 /// ```
543 /// let tuple = ("hello", 5, 'c');
544 ///
545 /// assert_eq!(tuple.0, "hello");
546 /// ```
547 ///
548 /// Tuples are often used as a return type when you want to return more than
549 /// one value:
550 ///
551 /// ```
552 /// fn calculate_point() -> (i32, i32) {
553 /// // Don't do a calculation, that's not the point of the example
554 /// (4, 5)
555 /// }
556 ///
557 /// let point = calculate_point();
558 ///
559 /// assert_eq!(point.0, 4);
560 /// assert_eq!(point.1, 5);
561 ///
562 /// // Combining this with patterns can be nicer.
563 ///
564 /// let (x, y) = calculate_point();
565 ///
566 /// assert_eq!(x, 4);
567 /// assert_eq!(y, 5);
568 /// ```
569 ///
570 #[stable(feature = "rust1", since = "1.0.0")]
571 mod prim_tuple { }
572
573 #[doc(primitive = "f32")]
574 /// The 32-bit floating point type.
575 ///
576 /// *[See also the `std::f32` module](f32/index.html).*
577 ///
578 #[stable(feature = "rust1", since = "1.0.0")]
579 mod prim_f32 { }
580
581 #[doc(primitive = "f64")]
582 //
583 /// The 64-bit floating point type.
584 ///
585 /// *[See also the `std::f64` module](f64/index.html).*
586 ///
587 #[stable(feature = "rust1", since = "1.0.0")]
588 mod prim_f64 { }
589
590 #[doc(primitive = "i8")]
591 //
592 /// The 8-bit signed integer type.
593 ///
594 /// *[See also the `std::i8` module](i8/index.html).*
595 ///
596 /// However, please note that examples are shared between primitive integer
597 /// types. So it's normal if you see usage of types like `i64` in there.
598 ///
599 #[stable(feature = "rust1", since = "1.0.0")]
600 mod prim_i8 { }
601
602 #[doc(primitive = "i16")]
603 //
604 /// The 16-bit signed integer type.
605 ///
606 /// *[See also the `std::i16` module](i16/index.html).*
607 ///
608 /// However, please note that examples are shared between primitive integer
609 /// types. So it's normal if you see usage of types like `i32` in there.
610 ///
611 #[stable(feature = "rust1", since = "1.0.0")]
612 mod prim_i16 { }
613
614 #[doc(primitive = "i32")]
615 //
616 /// The 32-bit signed integer type.
617 ///
618 /// *[See also the `std::i32` module](i32/index.html).*
619 ///
620 /// However, please note that examples are shared between primitive integer
621 /// types. So it's normal if you see usage of types like `i16` in there.
622 ///
623 #[stable(feature = "rust1", since = "1.0.0")]
624 mod prim_i32 { }
625
626 #[doc(primitive = "i64")]
627 //
628 /// The 64-bit signed integer type.
629 ///
630 /// *[See also the `std::i64` module](i64/index.html).*
631 ///
632 /// However, please note that examples are shared between primitive integer
633 /// types. So it's normal if you see usage of types like `i8` in there.
634 ///
635 #[stable(feature = "rust1", since = "1.0.0")]
636 mod prim_i64 { }
637
638 #[doc(primitive = "i128")]
639 //
640 /// The 128-bit signed integer type.
641 ///
642 /// *[See also the `std::i128` module](i128/index.html).*
643 ///
644 /// However, please note that examples are shared between primitive integer
645 /// types. So it's normal if you see usage of types like `i8` in there.
646 ///
647 #[unstable(feature = "i128", issue="35118")]
648 mod prim_i128 { }
649
650 #[doc(primitive = "u8")]
651 //
652 /// The 8-bit unsigned integer type.
653 ///
654 /// *[See also the `std::u8` module](u8/index.html).*
655 ///
656 /// However, please note that examples are shared between primitive integer
657 /// types. So it's normal if you see usage of types like `u64` in there.
658 ///
659 #[stable(feature = "rust1", since = "1.0.0")]
660 mod prim_u8 { }
661
662 #[doc(primitive = "u16")]
663 //
664 /// The 16-bit unsigned integer type.
665 ///
666 /// *[See also the `std::u16` module](u16/index.html).*
667 ///
668 /// However, please note that examples are shared between primitive integer
669 /// types. So it's normal if you see usage of types like `u32` in there.
670 ///
671 #[stable(feature = "rust1", since = "1.0.0")]
672 mod prim_u16 { }
673
674 #[doc(primitive = "u32")]
675 //
676 /// The 32-bit unsigned integer type.
677 ///
678 /// *[See also the `std::u32` module](u32/index.html).*
679 ///
680 /// However, please note that examples are shared between primitive integer
681 /// types. So it's normal if you see usage of types like `u16` in there.
682 ///
683 #[stable(feature = "rust1", since = "1.0.0")]
684 mod prim_u32 { }
685
686 #[doc(primitive = "u64")]
687 //
688 /// The 64-bit unsigned integer type.
689 ///
690 /// *[See also the `std::u64` module](u64/index.html).*
691 ///
692 /// However, please note that examples are shared between primitive integer
693 /// types. So it's normal if you see usage of types like `u8` in there.
694 ///
695 #[stable(feature = "rust1", since = "1.0.0")]
696 mod prim_u64 { }
697
698 #[doc(primitive = "u128")]
699 //
700 /// The 128-bit unsigned integer type.
701 ///
702 /// *[See also the `std::u128` module](u128/index.html).*
703 ///
704 /// However, please note that examples are shared between primitive integer
705 /// types. So it's normal if you see usage of types like `u8` in there.
706 ///
707 #[unstable(feature = "i128", issue="35118")]
708 mod prim_u128 { }
709
710 #[doc(primitive = "isize")]
711 //
712 /// The pointer-sized signed integer type.
713 ///
714 /// The size of this primitive is how many bytes it takes to reference any
715 /// location in memory. For example, on a 32 bit target, this is 4 bytes
716 /// and on a 64 bit target, this is 8 bytes.
717 ///
718 /// *[See also the `std::isize` module](isize/index.html).*
719 ///
720 /// However, please note that examples are shared between primitive integer
721 /// types. So it's normal if you see usage of types like `usize` in there.
722 ///
723 #[stable(feature = "rust1", since = "1.0.0")]
724 mod prim_isize { }
725
726 #[doc(primitive = "usize")]
727 //
728 /// The pointer-sized unsigned integer type.
729 ///
730 /// The size of this primitive is how many bytes it takes to reference any
731 /// location in memory. For example, on a 32 bit target, this is 4 bytes
732 /// and on a 64 bit target, this is 8 bytes.
733 ///
734 /// *[See also the `std::usize` module](usize/index.html).*
735 ///
736 /// However, please note that examples are shared between primitive integer
737 /// types. So it's normal if you see usage of types like `isize` in there.
738 ///
739 #[stable(feature = "rust1", since = "1.0.0")]
740 mod prim_usize { }
741
742 #[doc(primitive = "reference")]
743 //
744 /// References, both shared and mutable.
745 ///
746 /// A reference represents a borrow of some owned value. You can get one by using the `&` or `&mut`
747 /// operators on a value, or by using a `ref` or `ref mut` pattern.
748 ///
749 /// For those familiar with pointers, a reference is just a pointer that is assumed to not be null.
750 /// In fact, `Option<&T>` has the same memory representation as a nullable pointer, and can be
751 /// passed across FFI boundaries as such.
752 ///
753 /// In most cases, references can be used much like the original value. Field access, method
754 /// calling, and indexing work the same (save for mutability rules, of course). In addition, the
755 /// comparison operators transparently defer to the referent's implementation, allowing references
756 /// to be compared the same as owned values.
757 ///
758 /// References have a lifetime attached to them, which represents the scope for which the borrow is
759 /// valid. A lifetime is said to "outlive" another one if its representative scope is as long or
760 /// longer than the other. The `'static` lifetime is the longest lifetime, which represents the
761 /// total life of the program. For example, string literals have a `'static` lifetime because the
762 /// text data is embedded into the binary of the program, rather than in an allocation that needs
763 /// to be dynamically managed.
764 ///
765 /// `&mut T` references can be freely coerced into `&T` references with the same referent type, and
766 /// references with longer lifetimes can be freely coerced into references with shorter ones.
767 ///
768 /// For more information on how to use references, see [the book's section on "References and
769 /// Borrowing"][book-refs].
770 ///
771 /// [book-refs]: ../book/second-edition/ch04-02-references-and-borrowing.html
772 ///
773 /// The following traits are implemented for all `&T`, regardless of the type of its referent:
774 ///
775 /// * [`Copy`]
776 /// * [`Clone`] \(Note that this will not defer to `T`'s `Clone` implementation if it exists!)
777 /// * [`Deref`]
778 /// * [`Borrow`]
779 /// * [`Pointer`]
780 ///
781 /// [`Copy`]: marker/trait.Copy.html
782 /// [`Clone`]: clone/trait.Clone.html
783 /// [`Deref`]: ops/trait.Deref.html
784 /// [`Borrow`]: borrow/trait.Borrow.html
785 /// [`Pointer`]: fmt/trait.Pointer.html
786 ///
787 /// `&mut T` references get all of the above except `Copy` and `Clone` (to prevent creating
788 /// multiple simultaneous mutable borrows), plus the following, regardless of the type of its
789 /// referent:
790 ///
791 /// * [`DerefMut`]
792 /// * [`BorrowMut`]
793 ///
794 /// [`DerefMut`]: ops/trait.DerefMut.html
795 /// [`BorrowMut`]: borrow/trait.BorrowMut.html
796 ///
797 /// The following traits are implemented on `&T` references if the underlying `T` also implements
798 /// that trait:
799 ///
800 /// * All the traits in [`std::fmt`] except [`Pointer`] and [`fmt::Write`]
801 /// * [`PartialOrd`]
802 /// * [`Ord`]
803 /// * [`PartialEq`]
804 /// * [`Eq`]
805 /// * [`AsRef`]
806 /// * [`Fn`] \(in addition, `&T` references get [`FnMut`] and [`FnOnce`] if `T: Fn`)
807 /// * [`Hash`]
808 /// * [`ToSocketAddrs`]
809 ///
810 /// [`std::fmt`]: fmt/index.html
811 /// [`fmt::Write`]: fmt/trait.Write.html
812 /// [`PartialOrd`]: cmp/trait.PartialOrd.html
813 /// [`Ord`]: cmp/trait.Ord.html
814 /// [`PartialEq`]: cmp/trait.PartialEq.html
815 /// [`Eq`]: cmp/trait.Eq.html
816 /// [`AsRef`]: convert/trait.AsRef.html
817 /// [`Fn`]: ops/trait.Fn.html
818 /// [`FnMut`]: ops/trait.FnMut.html
819 /// [`FnOnce`]: ops/trait.FnOnce.html
820 /// [`Hash`]: hash/trait.Hash.html
821 /// [`ToSocketAddrs`]: net/trait.ToSocketAddrs.html
822 ///
823 /// `&mut T` references get all of the above except `ToSocketAddrs`, plus the following, if `T`
824 /// implements that trait:
825 ///
826 /// * [`AsMut`]
827 /// * [`FnMut`] \(in addition, `&mut T` references get [`FnOnce`] if `T: FnMut`)
828 /// * [`fmt::Write`]
829 /// * [`Iterator`]
830 /// * [`DoubleEndedIterator`]
831 /// * [`ExactSizeIterator`]
832 /// * [`FusedIterator`]
833 /// * [`TrustedLen`]
834 /// * [`Send`] \(note that `&T` references only get `Send` if `T: Sync`)
835 /// * [`io::Write`]
836 /// * [`Read`]
837 /// * [`Seek`]
838 /// * [`BufRead`]
839 ///
840 /// [`AsMut`]: convert/trait.AsMut.html
841 /// [`Iterator`]: iter/trait.Iterator.html
842 /// [`DoubleEndedIterator`]: iter/trait.DoubleEndedIterator.html
843 /// [`ExactSizeIterator`]: iter/trait.ExactSizeIterator.html
844 /// [`FusedIterator`]: iter/trait.FusedIterator.html
845 /// [`TrustedLen`]: iter/trait.TrustedLen.html
846 /// [`Send`]: marker/trait.Send.html
847 /// [`io::Write`]: io/trait.Write.html
848 /// [`Read`]: io/trait.Read.html
849 /// [`Seek`]: io/trait.Seek.html
850 /// [`BufRead`]: io/trait.BufRead.html
851 ///
852 /// Note that due to method call deref coercion, simply calling a trait method will act like they
853 /// work on references as well as they do on owned values! The implementations described here are
854 /// meant for generic contexts, where the final type `T` is a type parameter or otherwise not
855 /// locally known.
856 #[stable(feature = "rust1", since = "1.0.0")]
857 mod prim_ref { }
858
859 #[doc(primitive = "fn")]
860 //
861 /// Function pointers, like `fn(usize) -> bool`.
862 ///
863 /// *See also the traits [`Fn`], [`FnMut`], and [`FnOnce`].*
864 ///
865 /// [`Fn`]: ops/trait.Fn.html
866 /// [`FnMut`]: ops/trait.FnMut.html
867 /// [`FnOnce`]: ops/trait.FnOnce.html
868 ///
869 /// Plain function pointers are obtained by casting either plain functions, or closures that don't
870 /// capture an environment:
871 ///
872 /// ```
873 /// fn add_one(x: usize) -> usize {
874 /// x + 1
875 /// }
876 ///
877 /// let ptr: fn(usize) -> usize = add_one;
878 /// assert_eq!(ptr(5), 6);
879 ///
880 /// let clos: fn(usize) -> usize = |x| x + 5;
881 /// assert_eq!(clos(5), 10);
882 /// ```
883 ///
884 /// In addition to varying based on their signature, function pointers come in two flavors: safe
885 /// and unsafe. Plain `fn()` function pointers can only point to safe functions,
886 /// while `unsafe fn()` function pointers can point to safe or unsafe functions.
887 ///
888 /// ```
889 /// fn add_one(x: usize) -> usize {
890 /// x + 1
891 /// }
892 ///
893 /// unsafe fn add_one_unsafely(x: usize) -> usize {
894 /// x + 1
895 /// }
896 ///
897 /// let safe_ptr: fn(usize) -> usize = add_one;
898 ///
899 /// //ERROR: mismatched types: expected normal fn, found unsafe fn
900 /// //let bad_ptr: fn(usize) -> usize = add_one_unsafely;
901 ///
902 /// let unsafe_ptr: unsafe fn(usize) -> usize = add_one_unsafely;
903 /// let really_safe_ptr: unsafe fn(usize) -> usize = add_one;
904 /// ```
905 ///
906 /// On top of that, function pointers can vary based on what ABI they use. This is achieved by
907 /// adding the `extern` keyword to the type name, followed by the ABI in question. For example,
908 /// `fn()` is different from `extern "C" fn()`, which itself is different from `extern "stdcall"
909 /// fn()`, and so on for the various ABIs that Rust supports. Non-`extern` functions have an ABI
910 /// of `"Rust"`, and `extern` functions without an explicit ABI have an ABI of `"C"`. For more
911 /// information, see [the nomicon's section on foreign calling conventions][nomicon-abi].
912 ///
913 /// [nomicon-abi]: ../nomicon/ffi.html#foreign-calling-conventions
914 ///
915 /// Extern function declarations with the "C" or "cdecl" ABIs can also be *variadic*, allowing them
916 /// to be called with a variable number of arguments. Normal rust functions, even those with an
917 /// `extern "ABI"`, cannot be variadic. For more information, see [the nomicon's section on
918 /// variadic functions][nomicon-variadic].
919 ///
920 /// [nomicon-variadic]: ../nomicon/ffi.html#variadic-functions
921 ///
922 /// These markers can be combined, so `unsafe extern "stdcall" fn()` is a valid type.
923 ///
924 /// Like references in rust, function pointers are assumed to not be null, so if you want to pass a
925 /// function pointer over FFI and be able to accommodate null pointers, make your type
926 /// `Option<fn()>` with your required signature.
927 ///
928 /// Function pointers implement the following traits:
929 ///
930 /// * [`Clone`]
931 /// * [`PartialEq`]
932 /// * [`Eq`]
933 /// * [`PartialOrd`]
934 /// * [`Ord`]
935 /// * [`Hash`]
936 /// * [`Pointer`]
937 /// * [`Debug`]
938 ///
939 /// [`Clone`]: clone/trait.Clone.html
940 /// [`PartialEq`]: cmp/trait.PartialEq.html
941 /// [`Eq`]: cmp/trait.Eq.html
942 /// [`PartialOrd`]: cmp/trait.PartialOrd.html
943 /// [`Ord`]: cmp/trait.Ord.html
944 /// [`Hash`]: hash/trait.Hash.html
945 /// [`Pointer`]: fmt/trait.Pointer.html
946 /// [`Debug`]: fmt/trait.Debug.html
947 ///
948 /// Due to a temporary restriction in Rust's type system, these traits are only implemented on
949 /// functions that take 12 arguments or less, with the `"Rust"` and `"C"` ABIs. In the future, this
950 /// may change.
951 ///
952 /// In addition, function pointers of *any* signature, ABI, or safety are [`Copy`], and all *safe*
953 /// function pointers implement [`Fn`], [`FnMut`], and [`FnOnce`]. This works because these traits
954 /// are specially known to the compiler.
955 ///
956 /// [`Copy`]: marker/trait.Copy.html
957 #[stable(feature = "rust1", since = "1.0.0")]
958 mod prim_fn { }