liveBook · Manning

Inner attribute

As explained in Learn Rust in a Month of Lunches

An inner attribute is a Rust attribute written with #! that applies to the contents of its enclosing file or module.

An inner attribute is a Rust attribute written with #! that affects the contents of its enclosing file or module. Unlike an outer attribute, which applies to the item that follows it, an inner attribute applies inward. section 13.1.5

Remember these practical rules:

  • Place an inner attribute at the very top of its file or module, before other code. section 13.1.5
  • Use #![allow(unused)] when you want to silence warnings about unused variables and dead code. section 13.1.5
  • If the attribute appears too late, the compiler reports that an inner attribute is not permitted in that context. section 13.1.5

Silencing unused-code warnings

Place the attribute before the rest of the file: #![allow(dead_code)] and #![allow(unused_variables)]. The compiler can then remain quiet about unused structs and variables while the code is being developed. section 13.1.5

fn empty_function() {}
 
#![allow(dead_code)]

A minimal listing shows the required #! syntax and its file-level placement. section 13.1.5

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines scope and placement, demonstrates warning suppression, and stops short of detailed module mechanics.
Examples
Uses #![allow(dead_code)], #![allow(unused_variables)], and the combined #![allow(unused)] form.
What it assumes
Assumes basic Rust code and introduces attributes, while deferring detailed treatment of files and modules.
What you can do after
Apply it by placing #! attributes at the top of a file or module and interpreting compiler placement feedback.
How it connects
Connects inner attributes with outer attributes, attributes, modules, compiler warnings, and dead code.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.