Skip to content

Commit c0812c8

Browse files
committed
Rollup merge of rust-lang#44160 - AndyGauge:api-docs-macros, r=steveklabnik
API docs: macros. Standard Documentation Checklist Fixes rust-lang#29381 r? @steveklabnik
2 parents 8962393 + 80d513a commit c0812c8

File tree

3 files changed

+165
-42
lines changed

3 files changed

+165
-42
lines changed

src/liballoc/macros.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
/// Creates a `Vec` containing the arguments.
11+
/// Creates a [`Vec`] containing the arguments.
1212
///
1313
/// `vec!` allows `Vec`s to be defined with the same syntax as array expressions.
1414
/// There are two forms of this macro:
1515
///
16-
/// - Create a `Vec` containing a given list of elements:
16+
/// - Create a [`Vec`] containing a given list of elements:
1717
///
1818
/// ```
1919
/// let v = vec![1, 2, 3];
@@ -22,22 +22,25 @@
2222
/// assert_eq!(v[2], 3);
2323
/// ```
2424
///
25-
/// - Create a `Vec` from a given element and size:
25+
/// - Create a [`Vec`] from a given element and size:
2626
///
2727
/// ```
2828
/// let v = vec![1; 3];
2929
/// assert_eq!(v, [1, 1, 1]);
3030
/// ```
3131
///
3232
/// Note that unlike array expressions this syntax supports all elements
33-
/// which implement `Clone` and the number of elements doesn't have to be
33+
/// which implement [`Clone`] and the number of elements doesn't have to be
3434
/// a constant.
3535
///
36-
/// This will use `clone()` to duplicate an expression, so one should be careful
36+
/// This will use `clone` to duplicate an expression, so one should be careful
3737
/// using this with types having a nonstandard `Clone` implementation. For
3838
/// example, `vec![Rc::new(1); 5]` will create a vector of five references
3939
/// to the same boxed integer value, not five references pointing to independently
4040
/// boxed integers.
41+
///
42+
/// [`Vec`]: ../std/vec/struct.Vec.html
43+
/// [`Clone`]: ../std/clone/trait.Clone.html
4144
#[cfg(not(test))]
4245
#[macro_export]
4346
#[stable(feature = "rust1", since = "1.0.0")]
@@ -67,10 +70,22 @@ macro_rules! vec {
6770
($($x:expr,)*) => (vec![$($x),*])
6871
}
6972

70-
/// Use the syntax described in `std::fmt` to create a value of type `String`.
71-
/// See [`std::fmt`][fmt] for more information.
73+
/// Creates a `String` using interpolation of runtime expressions.
74+
///
75+
/// The first argument `format!` recieves is a format string. This must be a string
76+
/// literal. The power of the formatting string is in the `{}`s contained.
77+
///
78+
/// Additional parameters passed to `format!` replace the `{}`s within the
79+
/// formatting string in the order given unless named or positional parameters
80+
/// are used, see [`std::fmt`][fmt] for more information.
81+
///
82+
/// A common use for `format!` is concatenation and interpolation of strings.
83+
/// The same convention is used with [`print!`] and [`write!`] macros,
84+
/// depending on the intended destination of the string.
7285
///
7386
/// [fmt]: ../std/fmt/index.html
87+
/// [`print!`]: ../std/macro.print.html
88+
/// [`write!`]: ../std/macro.write.html
7489
///
7590
/// # Panics
7691
///

src/libcore/macros.rs

+42-14
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ macro_rules! panic {
6262
/// # Custom Messages
6363
///
6464
/// This macro has a second form, where a custom panic message can
65-
/// be provided with or without arguments for formatting.
65+
/// be provided with or without arguments for formatting. See [`std::fmt`]
66+
/// for syntax for this form.
6667
///
6768
/// [`panic!`]: macro.panic.html
6869
/// [`debug_assert!`]: macro.debug_assert.html
69-
/// [testing]: ../book/first-edition/testing.html
70+
/// [testing]: ../book/second-edition/ch11-01-writing-tests.html#checking-results-with-the-assert-macro
71+
/// [`std::fmt`]: ../std/fmt/index.html
7072
///
7173
/// # Examples
7274
///
@@ -252,13 +254,15 @@ macro_rules! debug_assert {
252254
/// On panic, this macro will print the values of the expressions with their
253255
/// debug representations.
254256
///
255-
/// Unlike `assert_eq!`, `debug_assert_eq!` statements are only enabled in non
257+
/// Unlike [`assert_eq!`], `debug_assert_eq!` statements are only enabled in non
256258
/// optimized builds by default. An optimized build will omit all
257259
/// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the
258260
/// compiler. This makes `debug_assert_eq!` useful for checks that are too
259261
/// expensive to be present in a release build but may be helpful during
260262
/// development.
261263
///
264+
/// [`assert_eq!`]: ../std/macro.assert_eq.html
265+
///
262266
/// # Examples
263267
///
264268
/// ```
@@ -277,13 +281,15 @@ macro_rules! debug_assert_eq {
277281
/// On panic, this macro will print the values of the expressions with their
278282
/// debug representations.
279283
///
280-
/// Unlike `assert_ne!`, `debug_assert_ne!` statements are only enabled in non
284+
/// Unlike [`assert_ne!`], `debug_assert_ne!` statements are only enabled in non
281285
/// optimized builds by default. An optimized build will omit all
282286
/// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the
283287
/// compiler. This makes `debug_assert_ne!` useful for checks that are too
284288
/// expensive to be present in a release build but may be helpful during
285289
/// development.
286290
///
291+
/// [`assert_ne!`]: ../std/macro.assert_ne.html
292+
///
287293
/// # Examples
288294
///
289295
/// ```
@@ -300,10 +306,9 @@ macro_rules! debug_assert_ne {
300306
/// Helper macro for reducing boilerplate code for matching `Result` together
301307
/// with converting downstream errors.
302308
///
303-
/// Prefer using `?` syntax to `try!`. `?` is built in to the language and is
304-
/// more succinct than `try!`. It is the standard method for error propagation.
309+
/// The `?` operator was added to replace `try!` and should be used instead.
305310
///
306-
/// `try!` matches the given `Result`. In case of the `Ok` variant, the
311+
/// `try!` matches the given [`Result`]. In case of the `Ok` variant, the
307312
/// expression has the value of the wrapped value.
308313
///
309314
/// In case of the `Err` variant, it retrieves the inner error. `try!` then
@@ -312,7 +317,9 @@ macro_rules! debug_assert_ne {
312317
/// error is then immediately returned.
313318
///
314319
/// Because of the early return, `try!` can only be used in functions that
315-
/// return `Result`.
320+
/// return [`Result`].
321+
///
322+
/// [`Result`]: ../std/result/enum.Result.html
316323
///
317324
/// # Examples
318325
///
@@ -331,20 +338,26 @@ macro_rules! debug_assert_ne {
331338
/// }
332339
/// }
333340
///
341+
/// // The prefered method of quick returning Errors
342+
/// fn write_to_file_question() -> Result<(), MyError> {
343+
/// let mut file = File::create("my_best_friends.txt")?;
344+
/// Ok(())
345+
/// }
346+
///
347+
/// // The previous method of quick returning Errors
334348
/// fn write_to_file_using_try() -> Result<(), MyError> {
335349
/// let mut file = try!(File::create("my_best_friends.txt"));
336350
/// try!(file.write_all(b"This is a list of my best friends."));
337-
/// println!("I wrote to the file");
338351
/// Ok(())
339352
/// }
353+
///
340354
/// // This is equivalent to:
341355
/// fn write_to_file_using_match() -> Result<(), MyError> {
342356
/// let mut file = try!(File::create("my_best_friends.txt"));
343357
/// match file.write_all(b"This is a list of my best friends.") {
344358
/// Ok(v) => v,
345359
/// Err(e) => return Err(From::from(e)),
346360
/// }
347-
/// println!("I wrote to the file");
348361
/// Ok(())
349362
/// }
350363
/// ```
@@ -365,7 +378,7 @@ macro_rules! try {
365378
/// formatted according to the specified format string and the result will be passed to the writer.
366379
/// The writer may be any value with a `write_fmt` method; generally this comes from an
367380
/// implementation of either the [`std::fmt::Write`] or the [`std::io::Write`] trait. The macro
368-
/// returns whatever the 'write_fmt' method returns; commonly a [`std::fmt::Result`], or an
381+
/// returns whatever the `write_fmt` method returns; commonly a [`std::fmt::Result`], or an
369382
/// [`io::Result`].
370383
///
371384
/// See [`std::fmt`] for more information on the format string syntax.
@@ -470,10 +483,20 @@ macro_rules! writeln {
470483
/// * Loops that dynamically terminate.
471484
/// * Iterators that dynamically terminate.
472485
///
486+
/// If the determination that the code is unreachable proves incorrect, the
487+
/// program immediately terminates with a [`panic!`]. The function [`unreachable`],
488+
/// which belongs to the [`std::intrinsics`] module, informs the compilier to
489+
/// optimize the code out of the release version entirely.
490+
///
491+
/// [`panic!`]: ../std/macro.panic.html
492+
/// [`unreachable`]: ../std/intrinsics/fn.unreachable.html
493+
/// [`std::intrinsics`]: ../std/intrinsics/index.html
494+
///
473495
/// # Panics
474496
///
475-
/// This will always [panic!](macro.panic.html)
497+
/// This will always [`panic!`]
476498
///
499+
/// [`panic!`]: ../std/macro.panic.html
477500
/// # Examples
478501
///
479502
/// Match arms:
@@ -516,13 +539,18 @@ macro_rules! unreachable {
516539
});
517540
}
518541

519-
/// A standardized placeholder for marking unfinished code. It panics with the
520-
/// message `"not yet implemented"` when executed.
542+
/// A standardized placeholder for marking unfinished code.
543+
///
544+
/// It panics with the message `"not yet implemented"` when executed.
521545
///
522546
/// This can be useful if you are prototyping and are just looking to have your
523547
/// code typecheck, or if you're implementing a trait that requires multiple
524548
/// methods, and you're only planning on using one of them.
525549
///
550+
/// # Panics
551+
///
552+
/// This macro always panics.
553+
///
526554
/// # Examples
527555
///
528556
/// Here's an example of some in-progress code. We have a trait `Foo`:

0 commit comments

Comments
 (0)