Expand description
Syntax tree traversal to walk a shared borrow of a syntax tree.
Each method of the Visit trait is a hook that can be overridden to
customize the behavior when visiting the corresponding type of node. By
default, every method recursively visits the substructure of the input
by invoking the right visitor method of each of its fields.
pub trait Visit<'ast> {
    /* ... */
    fn visit_expr_binary(&mut self, node: &'ast ExprBinary) {
        visit_expr_binary(self, node);
    }
    /* ... */
}
pub fn visit_expr_binary<'ast, V>(v: &mut V, node: &'ast ExprBinary)
where
    V: Visit<'ast> + ?Sized,
{
    for attr in &node.attrs {
        v.visit_attribute(attr);
    }
    v.visit_expr(&*node.left);
    v.visit_bin_op(&node.op);
    v.visit_expr(&*node.right);
}
/* ... */§Example
This visitor will print the name of every freestanding function in the syntax tree, including nested functions.
// [dependencies]
// quote = "1.0"
// syn = { version = "2.0", features = ["full", "visit"] }
use quote::quote;
use syn::visit::{self, Visit};
use syn::{File, ItemFn};
struct FnVisitor;
impl<'ast> Visit<'ast> for FnVisitor {
    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        println!("Function with name={}", node.sig.ident);
        // Delegate to the default impl to visit any nested functions.
        visit::visit_item_fn(self, node);
    }
}
fn main() {
    let code = quote! {
        pub fn f() {
            fn g() {}
        }
    };
    let syntax_tree: File = syn::parse2(code).unwrap();
    FnVisitor.visit_file(&syntax_tree);
}The 'ast lifetime on the input references means that the syntax tree
outlives the complete recursive visit call, so the visitor is allowed to
hold on to references into the syntax tree.
use quote::quote;
use syn::visit::{self, Visit};
use syn::{File, ItemFn};
struct FnVisitor<'ast> {
    functions: Vec<&'ast ItemFn>,
}
impl<'ast> Visit<'ast> for FnVisitor<'ast> {
    fn visit_item_fn(&mut self, node: &'ast ItemFn) {
        self.functions.push(node);
        visit::visit_item_fn(self, node);
    }
}
fn main() {
    let code = quote! {
        pub fn f() {
            fn g() {}
        }
    };
    let syntax_tree: File = syn::parse2(code).unwrap();
    let mut visitor = FnVisitor { functions: Vec::new() };
    visitor.visit_file(&syntax_tree);
    for f in visitor.functions {
        println!("Function with name={}", f.sig.ident);
    }
}Traits§
- Visit
- Syntax tree traversal to walk a shared borrow of a syntax tree.
Functions§
- visit_abi 
- visit_angle_ bracketed_ generic_ arguments 
- visit_arm 
- visit_assoc_ const 
- visit_assoc_ type 
- visit_attr_ style 
- visit_attribute 
- visit_bare_ fn_ arg 
- visit_bare_ variadic 
- visit_bin_ op 
- visit_block 
- visit_bound_ lifetimes 
- visit_captured_ param 
- visit_const_ param 
- visit_constraint 
- visit_data 
- visit_data_ enum 
- visit_data_ struct 
- visit_data_ union 
- visit_derive_ input 
- visit_expr 
- visit_expr_ array 
- visit_expr_ assign 
- visit_expr_ async 
- visit_expr_ await 
- visit_expr_ binary 
- visit_expr_ block 
- visit_expr_ break 
- visit_expr_ call 
- visit_expr_ cast 
- visit_expr_ closure 
- visit_expr_ const 
- visit_expr_ continue 
- visit_expr_ field 
- visit_expr_ for_ loop 
- visit_expr_ group 
- visit_expr_ if 
- visit_expr_ index 
- visit_expr_ infer 
- visit_expr_ let 
- visit_expr_ lit 
- visit_expr_ loop 
- visit_expr_ macro 
- visit_expr_ match 
- visit_expr_ method_ call 
- visit_expr_ paren 
- visit_expr_ path 
- visit_expr_ range 
- visit_expr_ raw_ addr 
- visit_expr_ reference 
- visit_expr_ repeat 
- visit_expr_ return 
- visit_expr_ struct 
- visit_expr_ try 
- visit_expr_ try_ block 
- visit_expr_ tuple 
- visit_expr_ unary 
- visit_expr_ unsafe 
- visit_expr_ while 
- visit_expr_ yield 
- visit_field 
- visit_field_ mutability 
- visit_field_ pat 
- visit_field_ value 
- visit_fields 
- visit_fields_ named 
- visit_fields_ unnamed 
- visit_file 
- visit_fn_ arg 
- visit_foreign_ item 
- visit_foreign_ item_ fn 
- visit_foreign_ item_ macro 
- visit_foreign_ item_ static 
- visit_foreign_ item_ type 
- visit_generic_ argument 
- visit_generic_ param 
- visit_generics 
- visit_ident 
- visit_impl_ item 
- visit_impl_ item_ const 
- visit_impl_ item_ fn 
- visit_impl_ item_ macro 
- visit_impl_ item_ type 
- visit_impl_ restriction 
- visit_index 
- visit_item 
- visit_item_ const 
- visit_item_ enum 
- visit_item_ extern_ crate 
- visit_item_ fn 
- visit_item_ foreign_ mod 
- visit_item_ impl 
- visit_item_ macro 
- visit_item_ mod 
- visit_item_ static 
- visit_item_ struct 
- visit_item_ trait 
- visit_item_ trait_ alias 
- visit_item_ type 
- visit_item_ union 
- visit_item_ use 
- visit_label 
- visit_lifetime 
- visit_lifetime_ param 
- visit_lit 
- visit_lit_ bool 
- visit_lit_ byte 
- visit_lit_ byte_ str 
- visit_lit_ char 
- visit_lit_ cstr 
- visit_lit_ float 
- visit_lit_ int 
- visit_lit_ str 
- visit_local 
- visit_local_ init 
- visit_macro 
- visit_macro_ delimiter 
- visit_member 
- visit_meta 
- visit_meta_ list 
- visit_meta_ name_ value 
- visit_parenthesized_ generic_ arguments 
- visit_pat 
- visit_pat_ ident 
- visit_pat_ or 
- visit_pat_ paren 
- visit_pat_ reference 
- visit_pat_ rest 
- visit_pat_ slice 
- visit_pat_ struct 
- visit_pat_ tuple 
- visit_pat_ tuple_ struct 
- visit_pat_ type 
- visit_pat_ wild 
- visit_path 
- visit_path_ arguments 
- visit_path_ segment 
- visit_pointer_ mutability 
- visit_precise_ capture 
- visit_predicate_ lifetime 
- visit_predicate_ type 
- visit_qself 
- visit_range_ limits 
- visit_receiver 
- visit_return_ type 
- visit_signature 
- visit_span 
- visit_static_ mutability 
- visit_stmt 
- visit_stmt_ macro 
- visit_trait_ bound 
- visit_trait_ bound_ modifier 
- visit_trait_ item 
- visit_trait_ item_ const 
- visit_trait_ item_ fn 
- visit_trait_ item_ macro 
- visit_trait_ item_ type 
- visit_type 
- visit_type_ array 
- visit_type_ bare_ fn 
- visit_type_ group 
- visit_type_ impl_ trait 
- visit_type_ infer 
- visit_type_ macro 
- visit_type_ never 
- visit_type_ param 
- visit_type_ param_ bound 
- visit_type_ paren 
- visit_type_ path 
- visit_type_ ptr 
- visit_type_ reference 
- visit_type_ slice 
- visit_type_ trait_ object 
- visit_type_ tuple 
- visit_un_ op 
- visit_use_ glob 
- visit_use_ group 
- visit_use_ name 
- visit_use_ path 
- visit_use_ rename 
- visit_use_ tree 
- visit_variadic 
- visit_variant 
- visit_vis_ restricted 
- visit_visibility 
- visit_where_ clause 
- visit_where_ predicate