Skip to content

Documentation request: include_str and format_args combination #85846

Open
@jkelleyrtp

Description

I noticed some weird behavior today and couldn't find any documentation on how it works. It seems that format_args! and include_str!can actually work together to do compile-time validation of file inputs. Like so:

let html = format_args!(include_str!("./index.html"), title="test");

Both of these are compiler builtins so their sources aren't easily accessible. However, in neither of their documentation did I see any mention of this behavior. Contrast this with a proc/decl macro that tries to parse the inners as a LitStr/literal:

macro_rules! parse_args {
    ($a:literal) => {};
}

fn test() {
    let nodes = parse_args!(include_str!("./htmlexample.html"));
}

with error:

   |
12 | macro_rules! parse_args {
   | ----------------------- when calling this macro
...
17 |     let nodes = parse_args!(include_str!("./htmlexample.html"));
   |                             ^^^^^^^^^^^ no rules expected this token in macro call

Is there any documentation for this behavior? Is there any way for macro developers to also leverage this mechanic for parse a str literal returned by include_str!?

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions