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.
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.
11 #[doc(primitive = "bool")]
17 #[doc(primitive = "char")]
19 /// A Unicode scalar value.
21 /// A `char` represents a
23 /// value](http://www.unicode.org/glossary/#unicode_scalar_value)*, as it can
24 /// contain any Unicode code point except high-surrogate and low-surrogate code
27 /// As such, only values in the ranges \[0x0,0xD7FF\] and \[0xE000,0x10FFFF\]
28 /// (inclusive) are allowed. A `char` can always be safely cast to a `u32`;
29 /// however the converse is not always true due to the above range limits
30 /// and, as such, should be performed via the `from_u32` function.
32 /// *[See also the `std::char` module](char/index.html).*
36 #[doc(primitive = "unit")]
38 /// The `()` type, sometimes called "unit" or "nil".
40 /// The `()` type has exactly one value `()`, and is used when there
41 /// is no other meaningful value that could be returned. `()` is most
42 /// commonly seen implicitly: functions without a `-> ...` implicitly
43 /// have return type `()`, that is, these are equivalent:
46 /// fn long() -> () {}
51 /// The semicolon `;` can be used to discard the result of an
52 /// expression at the end of a block, making the expression (and thus
53 /// the block) evaluate to `()`. For example,
56 /// fn returns_i64() -> i64 {
59 /// fn returns_unit() {
73 #[doc(primitive = "pointer")]
75 /// Raw, unsafe pointers, `*const T`, and `*mut T`.
77 /// Working with raw pointers in Rust is uncommon,
78 /// typically limited to a few patterns.
80 /// Use the `null` function to create null pointers, and the `is_null` method
81 /// of the `*const T` type to check for null. The `*const T` type also defines
82 /// the `offset` method, for pointer math.
84 /// # Common ways to create raw pointers
86 /// ## 1. Coerce a reference (`&T`) or mutable reference (`&mut T`).
89 /// let my_num: i32 = 10;
90 /// let my_num_ptr: *const i32 = &my_num;
91 /// let mut my_speed: i32 = 88;
92 /// let my_speed_ptr: *mut i32 = &mut my_speed;
95 /// To get a pointer to a boxed value, dereference the box:
98 /// let my_num: Box<i32> = Box::new(10);
99 /// let my_num_ptr: *const i32 = &*my_num;
100 /// let mut my_speed: Box<i32> = Box::new(88);
101 /// let my_speed_ptr: *mut i32 = &mut *my_speed;
104 /// This does not take ownership of the original allocation
105 /// and requires no resource management later,
106 /// but you must not use the pointer after its lifetime.
108 /// ## 2. Consume a box (`Box<T>`).
110 /// The `into_raw` function consumes a box and returns
111 /// the raw pointer. It doesn't destroy `T` or deallocate any memory.
114 /// let my_speed: Box<i32> = Box::new(88);
115 /// let my_speed: *mut i32 = Box::into_raw(my_speed);
117 /// // By taking ownership of the original `Box<T>` though
118 /// // we are obligated to put it together later to be destroyed.
120 /// drop(Box::from_raw(my_speed));
124 /// Note that here the call to `drop` is for clarity - it indicates
125 /// that we are done with the given value and it should be destroyed.
127 /// ## 3. Get it from C.
130 /// # #![feature(libc)]
131 /// extern crate libc;
137 /// let my_num: *mut i32 = libc::malloc(mem::size_of::<i32>() as libc::size_t) as *mut i32;
138 /// if my_num.is_null() {
139 /// panic!("failed to allocate memory");
141 /// libc::free(my_num as *mut libc::c_void);
146 /// Usually you wouldn't literally use `malloc` and `free` from Rust,
147 /// but C APIs hand out a lot of pointers generally, so are a common source
148 /// of raw pointers in Rust.
150 /// *[See also the `std::ptr` module](ptr/index.html).*
154 #[doc(primitive = "array")]
156 /// A fixed-size array, denoted `[T; N]`, for the element type, `T`, and
157 /// the non-negative compile time constant size, `N`.
159 /// Arrays values are created either with an explicit expression that lists
160 /// each element: `[x, y, z]` or a repeat expression: `[x; N]`. The repeat
161 /// expression requires that the element type is `Copy`.
163 /// The type `[T; N]` is `Copy` if `T: Copy`.
165 /// Arrays of sizes from 0 to 32 (inclusive) implement the following traits
166 /// if the element type allows it:
168 /// - `Clone` (only if `T: Copy`)
170 /// - `IntoIterator` (implemented for `&[T; N]` and `&mut [T; N]`)
171 /// - `PartialEq`, `PartialOrd`, `Ord`, `Eq`
173 /// - `AsRef`, `AsMut`
174 /// - `Borrow`, `BorrowMut`
177 /// Arrays dereference to [slices (`[T]`)][slice], so their methods can be called
180 /// [slice]: primitive.slice.html
182 /// Rust does not currently support generics over the size of an array type.
187 /// let mut array: [i32; 3] = [0; 3];
192 /// assert_eq!([1, 2], &array[1..]);
194 /// // This loop prints: 0 1 2
195 /// for x in &array {
196 /// print!("{} ", x);
203 #[doc(primitive = "slice")]
205 /// A dynamically-sized view into a contiguous sequence, `[T]`.
207 /// Slices are a view into a block of memory represented as a pointer and a
212 /// let vec = vec![1, 2, 3];
213 /// let int_slice = &vec[..];
214 /// // coercing an array to a slice
215 /// let str_slice: &[&str] = &["one", "two", "three"];
218 /// Slices are either mutable or shared. The shared slice type is `&[T]`,
219 /// while the mutable slice type is `&mut [T]`, where `T` represents the element
220 /// type. For example, you can mutate the block of memory that a mutable slice
224 /// let x = &mut [1, 2, 3];
226 /// assert_eq!(x, &[1, 7, 3]);
229 /// *[See also the `std::slice` module](slice/index.html).*
233 #[doc(primitive = "str")]
235 /// Unicode string slices.
237 /// Rust's `str` type is one of the core primitive types of the language. `&str`
238 /// is the borrowed string type. This type of string can only be created from
239 /// other strings, unless it is a `&'static str` (see below). It is not possible
240 /// to move out of borrowed strings because they are owned elsewhere.
244 /// Here's some code that uses a `&str`:
247 /// let s = "Hello, world.";
250 /// This `&str` is a `&'static str`, which is the type of string literals.
251 /// They're `'static` because literals are available for the entire lifetime of
254 /// You can get a non-`'static` `&str` by taking a slice of a `String`:
257 /// let some_string = "Hello, world.".to_string();
258 /// let s = &some_string;
263 /// Rust's string type, `str`, is a sequence of Unicode scalar values encoded as
264 /// a stream of UTF-8 bytes. All [strings](../../reference.html#literals) are
265 /// guaranteed to be validly encoded UTF-8 sequences. Additionally, strings are
266 /// not null-terminated and can thus contain null bytes.
268 /// The actual representation of `str`s have direct mappings to slices: `&str`
269 /// is the same as `&[u8]`.
271 /// *[See also the `std::str` module](str/index.html).*
275 #[doc(primitive = "tuple")]
277 /// A finite heterogeneous sequence, `(T, U, ..)`.
279 /// To access the _N_-th element of a tuple one can use `N` itself
280 /// as a field of the tuple.
282 /// Indexing starts from zero, so `0` returns first value, `1`
283 /// returns second value, and so on. In general, a tuple with _S_
284 /// elements provides aforementioned fields from `0` to `S-1`.
286 /// If every type inside a tuple implements one of the following
287 /// traits, then a tuple itself also implements it.
300 /// Accessing elements of a tuple at specified indices:
303 /// let x = ("colorless", "green", "ideas", "sleep", "furiously");
304 /// assert_eq!(x.3, "sleep");
308 /// assert_eq!(v.0 * u.0 + v.1 * u.1, -12);
311 /// Using traits implemented for tuples:
318 /// let c = b.clone();
321 /// let d : (u32, f32) = Default::default();
322 /// assert_eq!(d, (0, 0.0f32));
327 #[doc(primitive = "f32")]
328 /// The 32-bit floating point type.
330 /// *[See also the `std::f32` module](f32/index.html).*
334 #[doc(primitive = "f64")]
336 /// The 64-bit floating point type.
338 /// *[See also the `std::f64` module](f64/index.html).*
342 #[doc(primitive = "i8")]
344 /// The 8-bit signed integer type.
346 /// *[See also the `std::i8` module](i8/index.html).*
350 #[doc(primitive = "i16")]
352 /// The 16-bit signed integer type.
354 /// *[See also the `std::i16` module](i16/index.html).*
358 #[doc(primitive = "i32")]
360 /// The 32-bit signed integer type.
362 /// *[See also the `std::i32` module](i32/index.html).*
366 #[doc(primitive = "i64")]
368 /// The 64-bit signed integer type.
370 /// *[See also the `std::i64` module](i64/index.html).*
374 #[doc(primitive = "u8")]
376 /// The 8-bit unsigned integer type.
378 /// *[See also the `std::u8` module](u8/index.html).*
382 #[doc(primitive = "u16")]
384 /// The 16-bit unsigned integer type.
386 /// *[See also the `std::u16` module](u16/index.html).*
390 #[doc(primitive = "u32")]
392 /// The 32-bit unsigned integer type.
394 /// *[See also the `std::u32` module](u32/index.html).*
398 #[doc(primitive = "u64")]
400 /// The 64-bit unsigned integer type.
402 /// *[See also the `std::u64` module](u64/index.html).*
406 #[doc(primitive = "isize")]
408 /// The pointer-sized signed integer type.
410 /// *[See also the `std::isize` module](isize/index.html).*
414 #[doc(primitive = "usize")]
416 /// The pointer-sized unsigned integer type.
418 /// *[See also the `std::usize` module](usize/index.html).*