macro_rules! find_attr {
($tcx: expr, crate, $pattern: pat $(if $guard: expr)?) => { ... };
($tcx: expr, crate, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
($tcx: expr, $id: expr, $pattern: pat $(if $guard: expr)?) => { ... };
($tcx: expr, $id: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
($attributes_list: expr, $pattern: pat $(if $guard: expr)?) => { ... };
($attributes_list: expr, $pattern: pat $(if $guard: expr)? => $e: expr) => { ... };
}Expand description
Finds attributes by pattern matching.
A little like matches but for attributes.
Note that this macro accepts several “id” types: DefId, LocalDefId, OwnerId and
HirId.
§Examples
It is most commonly used to check whether something has an attribute or to get its contents if it is present:
ⓘ
let is_naked: bool = find_attr!(tcx, def_id, Naked(..));
let is_visible: bool = find_attr!(tcx, def_id, Doc(doc) if doc.hidden.is_none());
let link_name: Option<Symbol> = find_attr!(tcx, def_id, LinkName { name, .. } => *name);Another common case is finding attributes applied to the root of the current crate. For that, use the shortcut:
ⓘ
find_attr!(tcx, crate, <pattern>)If you already have a list of attributes in scope, you can also use that:
ⓘ
let attrs = <list of attributes>;
// finds the repr attribute
if let Some(r) = find_attr!(attrs, Repr(r) => r) {
}
// checks if one has matched
if find_attr!(attrs, Repr(_)) {
}