Skip to content

Commit

Permalink
auto merge of #14513 : alexcrichton/rust/rustdoc-primitives, r=huonw
Browse files Browse the repository at this point in the history
This is currently rebased on top of #14478, but that's just to preemptively avoid rebase conflicts and to provide a better preview. This can land independently of that PR.

This change crates a dedicated page in rustdoc for primitive types to outline everything you can do with them (at least in a basic way).

* Preview - http://people.mozilla.org/~acrichton/doc/
* Exhibit A - http://people.mozilla.org/~acrichton/doc/std/#primitives
* Exhibit B - http://people.mozilla.org/~acrichton/doc/std/primitive.str.html
* Exhibit C - http://people.mozilla.org/~acrichton/doc/std/primitive.slice.html

Please don't hesitate to be nitpickity, it's easy to overlook a thing here or there!
  • Loading branch information
bors committed Jun 1, 2014
2 parents 5527c5d + d58f27a commit 4e0b936
Show file tree
Hide file tree
Showing 49 changed files with 865 additions and 384 deletions.
37 changes: 19 additions & 18 deletions src/doc/complement-cheatsheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

**Int to string**

Use [`ToStr`](../std/to_str/trait.ToStr.html).
Use [`ToStr`](std/to_str/trait.ToStr.html).

~~~
let x: int = 42;
Expand All @@ -13,8 +13,8 @@ let y: String = x.to_str().to_string();

**String to int**

Use [`FromStr`](../std/from_str/trait.FromStr.html), and its helper function,
[`from_str`](../std/from_str/fn.from_str.html).
Use [`FromStr`](std/from_str/trait.FromStr.html), and its helper function,
[`from_str`](std/from_str/fn.from_str.html).

~~~
let x: Option<int> = from_str("42");
Expand All @@ -35,8 +35,8 @@ let y: String = format!("{:X}", x); // uppercase hexadecimal

**String to int, in non-base-10**

Use [`FromStrRadix`](../std/num/trait.FromStrRadix.html), and its helper
function, [`from_str_radix`](../std/num/fn.from_str_radix.html).
Use [`FromStrRadix`](std/num/trait.FromStrRadix.html), and its helper
function, [`from_str_radix`](std/num/fn.from_str_radix.html).

~~~
use std::num;
Expand All @@ -48,7 +48,7 @@ let y: i64 = x.unwrap();
**Vector of Bytes to String**

To return a Borrowed String Slice (&str) use the str helper function
[`from_utf8`](../std/str/fn.from_utf8.html).
[`from_utf8`](std/str/fn.from_utf8.html).

~~~
use std::str;
Expand All @@ -58,7 +58,7 @@ let x: &str = str::from_utf8(bytes).unwrap();
~~~

To return an Owned String use the str helper function
[`from_utf8_owned`](../std/str/fn.from_utf8_owned.html).
[`from_utf8_owned`](std/str/fn.from_utf8_owned.html).

~~~
use std::str;
Expand All @@ -68,8 +68,8 @@ let x: Option<String> =
let y: String = x.unwrap();
~~~

To return a [`MaybeOwned`](../std/str/enum.MaybeOwned.html) use the str helper
function [`from_utf8_lossy`](../std/str/fn.from_utf8_owned.html).
To return a [`MaybeOwned`](std/str/type.MaybeOwned.html) use the str helper
function [`from_utf8_lossy`](std/str/fn.from_utf8_owned.html).
This function also replaces non-valid utf-8 sequences with U+FFFD replacement
character.

Expand All @@ -85,11 +85,11 @@ let y = str::from_utf8_lossy(x);
## How do I read from a file?

Use
[`File::open`](../std/io/fs/struct.File.html#method.open)
[`File::open`](std/io/fs/struct.File.html#method.open)
to create a
[`File`](../std/io/fs/struct.File.html)
[`File`](std/io/fs/struct.File.html)
struct, which implements the
[`Reader`](../std/io/trait.Reader.html)
[`Reader`](std/io/trait.Reader.html)
trait.

~~~ {.ignore}
Expand All @@ -103,7 +103,8 @@ let reader : File = File::open(&path).unwrap_or_else(on_error);

## How do I iterate over the lines in a file?

Use the [`lines`](../std/io/trait.Buffer.html#method.lines) method on a [`BufferedReader`](../std/io/buffered/struct.BufferedReader.html).
Use the [`lines`](std/io/trait.Buffer.html#method.lines) method on a
[`BufferedReader`](std/io/struct.BufferedReader.html).

~~~
use std::io::BufferedReader;
Expand All @@ -121,7 +122,7 @@ for line in reader.lines() {

## How do I search for a substring?

Use the [`find_str`](../std/str/trait.StrSlice.html#tymethod.find_str) method.
Use the [`find_str`](std/str/trait.StrSlice.html#tymethod.find_str) method.

~~~
let str = "Hello, this is some random string";
Expand All @@ -132,7 +133,7 @@ let index: Option<uint> = str.find_str("rand");

## How do I get the length of a vector?

The [`Container`](../std/container/trait.Container.html) trait provides the `len` method.
The [`Container`](std/container/trait.Container.html) trait provides the `len` method.

~~~
let u: Vec<u32> = vec![0, 1, 2];
Expand All @@ -144,7 +145,7 @@ println!("u: {}, v: {}, w: {}", u.len(), v.len(), w.len()); // 3, 4, 5

## How do I iterate over a vector?

Use the [`iter`](../std/vec/trait.ImmutableVector.html#tymethod.iter) method.
Use the [`iter`](std/slice/trait.ImmutableVector.html#tymethod.iter) method.

~~~
let values: Vec<int> = vec![1, 2, 3, 4, 5];
Expand All @@ -153,9 +154,9 @@ for value in values.iter() { // value: &int
}
~~~

(See also [`mut_iter`](../std/vec/trait.MutableVector.html#tymethod.mut_iter)
(See also [`mut_iter`](std/slice/trait.MutableVector.html#tymethod.mut_iter)
which yields `&mut int` and
[`move_iter`](../std/vec/trait.OwnedVector.html#tymethod.move_iter) which yields
[`move_iter`](std/slice/trait.OwnedVector.html#tymethod.move_iter) which yields
`int` while consuming the `values` vector.)

# Type system
Expand Down
6 changes: 3 additions & 3 deletions src/doc/complement-lang-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Some examples that demonstrate different aspects of the language:
* The extra library's [json] module. Enums and pattern matching

[sprocketnes]: https://github.com/pcwalton/sprocketnes
[hash]: https://github.com/mozilla/rust/blob/master/src/libstd/hash.rs
[hash]: https://github.com/mozilla/rust/blob/master/src/libstd/hash/mod.rs
[HashMap]: https://github.com/mozilla/rust/blob/master/src/libcollections/hashmap.rs
[json]: https://github.com/mozilla/rust/blob/master/src/libserialize/json.rs

Expand Down Expand Up @@ -149,6 +149,6 @@ example we were setting RUST_LOG to the name of the hello crate. Multiple paths
can be combined to control the exact logging you want to see. For example, when
debugging linking in the compiler you might set
`RUST_LOG=rustc::metadata::creader,rustc::util::filesearch,rustc::back::rpath`
For a full description see [the language reference][1].
For a full description see [the logging crate][1].

[1]:http://doc.rust-lang.org/doc/master/rust.html#logging-system
[1]:log/index.html
9 changes: 5 additions & 4 deletions src/doc/guide-unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,9 @@ shouldn't get triggered.

The second of these two functions, `eh_personality`, is used by the failure
mechanisms of the compiler. This is often mapped to GCC's personality function
(see the [libstd implementation](../std/rt/unwind/) for more information), but
crates which do not trigger failure can be assured that this function is never
called.
(see the [libstd implementation](std/rt/unwind/index.html) for more
information), but crates which do not trigger failure can be assured that this
function is never called.

## Using libcore

Expand All @@ -511,7 +511,8 @@ called.
With the above techniques, we've got a bare-metal executable running some Rust
code. There is a good deal of functionality provided by the standard library,
however, that is necessary to be productive in Rust. If the standard library is
not sufficient, then [libcore](../core/) is designed to be used instead.
not sufficient, then [libcore](core/index.html) is designed to be used
instead.

The core library has very few dependencies and is much more portable than the
standard library itself. Additionally, the core library has most of the
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mod arc;
pub mod rc;

#[cfg(not(test))]
#[doc(hidden)]
mod std {
pub use core::fmt;
pub use core::option;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//!
//! A `to_bit` conversion function.

#![doc(primitive = "bool")]

use num::{Int, one, zero};

/////////////////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//! and, as such, should be performed via the `from_u32` function..

#![allow(non_snake_case_functions)]
#![doc(primitive = "char")]

use mem::transmute;
use option::{None, Option, Some};
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ pub mod fmt;
// crate.
mod should_not_exist;

#[doc(hidden)]
mod core {
pub use failure;
}

#[doc(hidden)]
mod std {
pub use clone;
pub use cmp;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

//! Operations and constants for 32-bits floats (`f32` type)

#![doc(primitive = "f32")]

use intrinsics;
use mem;
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

//! Operations and constants for 64-bits floats (`f64` type)

#![doc(primitive = "f64")]

use intrinsics;
use mem;
use num::{FPNormal, FPCategory, FPZero, FPSubnormal, FPInfinite, FPNaN};
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/num/i16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for signed 16-bits integers (`i16` type)

#![doc(primitive = "i16")]

int_module!(i16, 16)

2 changes: 2 additions & 0 deletions src/libcore/num/i32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for signed 32-bits integers (`i32` type)

#![doc(primitive = "i32")]

int_module!(i32, 32)

2 changes: 2 additions & 0 deletions src/libcore/num/i64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for signed 64-bits integers (`i64` type)

#![doc(primitive = "i64")]

int_module!(i64, 64)

2 changes: 2 additions & 0 deletions src/libcore/num/i8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for signed 8-bits integers (`i8` type)

#![doc(primitive = "i8")]

int_module!(i8, 8)

2 changes: 2 additions & 0 deletions src/libcore/num/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

//! Operations and constants for architecture-sized signed integers (`int` type)

#![doc(primitive = "int")]

#[cfg(target_word_size = "32")] int_module!(int, 32)
#[cfg(target_word_size = "64")] int_module!(int, 64)

2 changes: 2 additions & 0 deletions src/libcore/num/u16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

//! Operations and constants for unsigned 16-bits integers (`u16` type)

#![doc(primitive = "u16")]

uint_module!(u16, i16, 16)
2 changes: 2 additions & 0 deletions src/libcore/num/u32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for unsigned 32-bits integers (`u32` type)

#![doc(primitive = "u32")]

uint_module!(u32, i32, 32)

2 changes: 2 additions & 0 deletions src/libcore/num/u64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for unsigned 64-bits integer (`u64` type)

#![doc(primitive = "u64")]

uint_module!(u64, i64, 64)

2 changes: 2 additions & 0 deletions src/libcore/num/u8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for unsigned 8-bits integers (`u8` type)

#![doc(primitive = "u8")]

uint_module!(u8, i8, 8)

2 changes: 2 additions & 0 deletions src/libcore/num/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@

//! Operations and constants for architecture-sized unsigned integers (`uint` type)

#![doc(primitive = "uint")]

uint_module!(uint, int, ::int::BITS)

4 changes: 2 additions & 2 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
//! similar and complementary: they are often employed to indicate a
//! lack of a return value; and they are trivially converted between
//! each other, so `Result`s are often handled by first converting to
//! `Option` with the [`ok`](enum.Result.html#method.ok) and
//! [`err`](enum.Result.html#method.ok) methods.
//! `Option` with the [`ok`](type.Result.html#method.ok) and
//! [`err`](type.Result.html#method.ok) methods.
//!
//! Whereas `Option` only indicates the lack of a value, `Result` is
//! specifically for error reporting, and carries with it an error
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//!
//! For more details `std::slice`.

#![doc(primitive = "slice")]

use mem::transmute;
use clone::Clone;
use container::Container;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
//!
//! For more details, see std::str

#![doc(primitive = "str")]

use mem;
use char;
use clone::Clone;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
//! assert_eq!(d, (0u32, 0.0f32));
//! ```

#![doc(primitive = "tuple")]

use clone::Clone;
#[cfg(not(test))] use cmp::*;
#[cfg(not(test))] use default::Default;
Expand Down
Loading

0 comments on commit 4e0b936

Please sign in to comment.