Stream: rustdoc

Topic: macro_exports and std::prelude


view this post on Zulip Jeremiah Senkpiel (Jun 17 2021 at 17:37):

It's pretty confusing that no exported macros show up or are mentioned on https://doc.rust-lang.org/std/prelude/v1/index.html given that there are apparently 22 exported macros from the prelude (https://github.com/rust-lang/rfcs/blob/master/text/3114-prelude-2021.md#prior-art)

view this post on Zulip Jeremiah Senkpiel (Jun 17 2021 at 17:38):

The page at https://doc.rust-lang.org/std/prelude/index.html also does not mention any

view this post on Zulip Daniel Henry-Mantilla (Jun 17 2021 at 18:56):

I don't understand what you mean: on the site you linked, we can see several macros; on my machine, curl 'https://doc.rust-lang.org/std/prelude/v1/index.html' 2>&- | grep -o macro\\. | wc -l yields 31, for instance.

view this post on Zulip Daniel Henry-Mantilla (Jun 17 2021 at 18:57):

Screen-Shot-2021-06-17-at-20.57.45.png

view this post on Zulip Jeremiah Senkpiel (Jun 17 2021 at 19:02):

oh. True. Guess that's my bad in reading them.

Why is this less macros than is actually in the global scope though? Is it because the others are in core?

Also uh, maybe re-exports could be sorted/labeled better ...

view this post on Zulip Daniel Henry-Mantilla (Jun 17 2021 at 19:17):

Also uh, maybe re-exports could be sorted/labeled better ...

Yeah, I guess rustdoc is ordering the re-exported items by nature of the reexported item rather than mixing them all up

than in the global scope

Oh, I see what you mean now: macros such as println! (which are #[macro_export] macro_rules! macros) are then into "the prelude" not by virtue of the v1 prelude or whatnot, but actually because non-#![no_std] crates import the std crate through an implicit #[macro_use] extern crate std; statement (and #[macro_use] pulls the #[macro_export]ed macros into the prelude of the current crate).

This means that there should be a funny way to observe what you mean:

#![no_std]

extern crate std;

use ::std::prelude::v1::*;

fn main ()
{
    let _ = format_args!(""); // OK
    let _ = format!(""); // Error
}

yields:

error: cannot find macro `format` in this scope
  --> src/main.rs:10:13
   |
10 |     let _ = format!(""); // Error
   |             ^^^^^^
   |
   = note: consider importing this macro:
           std::format

(and the inverse would be to do #[macro_use] extern crate std; without the prelude import)

This is thus not a docs issue, but a libs one.


Last updated: Oct 11 2021 at 22:34 UTC