CTF-web: 查看rust代码抽象语法树

查看 Rust 抽象语法树(Abstract Syntax Tree, AST)是理解 Rust 代码结构和编译过程的重要步骤。

首先,安装 Nightly 工具链

bash 复制代码
rustup install nightly

然后可以查看rust文件抽象语法树

powershell 复制代码
rustc +nightly -Zunpretty=ast-tree file.rs

示例1

rust 复制代码
fn main() {  
    println!("Hello, world!");  
}
rust 复制代码
Crate {
    attrs: [],
    items: [
        Item {
            attrs: [],
            id: NodeId(4294967040),
            span: ./src/main.rs:1:1: 3:2 (#0),
            vis: Visibility {
                kind: Inherited,
                span: no-location (#0),
                tokens: None,
            },
            ident: main#0,
            kind: Fn(
                Fn {
                    defaultness: Final,
                    generics: Generics {
                        params: [],
                        where_clause: WhereClause {
                            has_where_token: false,
                            predicates: [],
                            span: ./src/main.rs:1:10: 1:10 (#0),
                        },
                        span: ./src/main.rs:1:8: 1:8 (#0),
                    },
                    sig: FnSig {
                        header: FnHeader {
                            safety: Default,
                            coroutine_kind: None,
                            constness: No,
                            ext: None,
                        },
                        decl: FnDecl {
                            inputs: [],
                            output: Default(
                                ./src/main.rs:1:10: 1:10 (#0),
                            ),
                        },
                        span: ./src/main.rs:1:1: 1:10 (#0),
                    },
                    contract: None,
                    body: Some(
                        Block {
                            stmts: [
                                Stmt {
                                    id: NodeId(4294967040),
                                    kind: MacCall(
                                        MacCallStmt {
                                            mac: MacCall {
                                                path: Path {
                                                    span: ./src/main.rs:2:5: 2:12 (#0),
                                                    segments: [
                                                        PathSegment {
                                                            ident: println#0,
                                                            id: NodeId(4294967040),
                                                            args: None,
                                                        },
                                                    ],
                                                    tokens: None,
                                                },
                                                args: DelimArgs {
                                                    dspan: DelimSpan {
                                                        open: ./src/main.rs:2:13: 2:14 (#0),
                                                        close: ./src/main.rs:2:29: 2:30 (#0),
                                                    },
                                                    delim: Parenthesis,
                                                    tokens: TokenStream(
                                                        [
                                                            Token(
                                                                Token {
                                                                    kind: Literal(
                                                                        Lit {
                                                                            kind: Str,
                                                                            symbol: "Hello, world!",
                                                                            suffix: None,
                                                                        },
                                                                    ),
                                                                    span: ./src/main.rs:2:14: 2:29 (#0),
                                                                },
                                                                JointHidden,
                                                            ),
                                                        ],
                                                    ),
                                                },
                                            },
                                            style: Semicolon,
                                            attrs: [],
                                            tokens: None,
                                        },
                                    ),
                                    span: ./src/main.rs:2:5: 2:31 (#0),
                                },
                            ],
                            id: NodeId(4294967040),
                            rules: Default,
                            span: ./src/main.rs:1:11: 3:2 (#0),
                            tokens: None,
                            could_be_bare_literal: false,
                        },
                    ),
                },
            ),
            tokens: None,
        },
    ],
    spans: ModSpans {
        inner_span: ./src/main.rs:1:1: 3:2 (#0),
        inject_use_span: no-location (#0),
    },
    id: NodeId(4294967040),
    is_placeholder: false,
}

示例2

rust 复制代码
fn main() {  
    println!("test1");  
    println!("test2");  
}
rust 复制代码
Crate {
    attrs: [],
    items: [
        Item {
            attrs: [],
            id: NodeId(4294967040),
            span: ./src/main.rs:1:1: 4:2 (#0),
            vis: Visibility {
                kind: Inherited,
                span: no-location (#0),
                tokens: None,
            },
            ident: main#0,
            kind: Fn(
                Fn {
                    defaultness: Final,
                    generics: Generics {
                        params: [],
                        where_clause: WhereClause {
                            has_where_token: false,
                            predicates: [],
                            span: ./src/main.rs:1:10: 1:10 (#0),
                        },
                        span: ./src/main.rs:1:8: 1:8 (#0),
                    },
                    sig: FnSig {
                        header: FnHeader {
                            safety: Default,
                            coroutine_kind: None,
                            constness: No,
                            ext: None,
                        },
                        decl: FnDecl {
                            inputs: [],
                            output: Default(
                                ./src/main.rs:1:10: 1:10 (#0),
                            ),
                        },
                        span: ./src/main.rs:1:1: 1:10 (#0),
                    },
                    contract: None,
                    body: Some(
                        Block {
                            stmts: [
                                Stmt {
                                    id: NodeId(4294967040),
                                    kind: MacCall(
                                        MacCallStmt {
                                            mac: MacCall {
                                                path: Path {
                                                    span: ./src/main.rs:2:5: 2:12 (#0),
                                                    segments: [
                                                        PathSegment {
                                                            ident: println#0,
                                                            id: NodeId(4294967040),
                                                            args: None,
                                                        },
                                                    ],
                                                    tokens: None,
                                                },
                                                args: DelimArgs {
                                                    dspan: DelimSpan {
                                                        open: ./src/main.rs:2:13: 2:14 (#0),
                                                        close: ./src/main.rs:2:21: 2:22 (#0),
                                                    },
                                                    delim: Parenthesis,
                                                    tokens: TokenStream(
                                                        [
                                                            Token(
                                                                Token {
                                                                    kind: Literal(
                                                                        Lit {
                                                                            kind: Str,
                                                                            symbol: "test1",
                                                                            suffix: None,
                                                                        },
                                                                    ),
                                                                    span: ./src/main.rs:2:14: 2:21 (#0),
                                                                },
                                                                JointHidden,
                                                            ),
                                                        ],
                                                    ),
                                                },
                                            },
                                            style: Semicolon,
                                            attrs: [],
                                            tokens: None,
                                        },
                                    ),
                                    span: ./src/main.rs:2:5: 2:23 (#0),
                                },
                                Stmt {
                                    id: NodeId(4294967040),
                                    kind: MacCall(
                                        MacCallStmt {
                                            mac: MacCall {
                                                path: Path {
                                                    span: ./src/main.rs:3:5: 3:12 (#0),
                                                    segments: [
                                                        PathSegment {
                                                            ident: println#0,
                                                            id: NodeId(4294967040),
                                                            args: None,
                                                        },
                                                    ],
                                                    tokens: None,
                                                },
                                                args: DelimArgs {
                                                    dspan: DelimSpan {
                                                        open: ./src/main.rs:3:13: 3:14 (#0),
                                                        close: ./src/main.rs:3:21: 3:22 (#0),
                                                    },
                                                    delim: Parenthesis,
                                                    tokens: TokenStream(
                                                        [
                                                            Token(
                                                                Token {
                                                                    kind: Literal(
                                                                        Lit {
                                                                            kind: Str,
                                                                            symbol: "test2",
                                                                            suffix: None,
                                                                        },
                                                                    ),
                                                                    span: ./src/main.rs:3:14: 3:21 (#0),
                                                                },
                                                                JointHidden,
                                                            ),
                                                        ],
                                                    ),
                                                },
                                            },
                                            style: Semicolon,
                                            attrs: [],
                                            tokens: None,
                                        },
                                    ),
                                    span: ./src/main.rs:3:5: 3:23 (#0),
                                },
                            ],
                            id: NodeId(4294967040),
                            rules: Default,
                            span: ./src/main.rs:1:11: 4:2 (#0),
                            tokens: None,
                            could_be_bare_literal: false,
                        },
                    ),
                },
            ),
            tokens: None,
        },
    ],
    spans: ModSpans {
        inner_span: ./src/main.rs:1:1: 4:2 (#0),
        inject_use_span: no-location (#0),
    },
    id: NodeId(4294967040),
    is_placeholder: false,
}
相关推荐
用户967151139167215 小时前
Rust 如何轻松实现 RTMP 流媒体推送?深入解析直播推流场景与解决方案
rust·ffmpeg
无名之逆16 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
s91236010116 小时前
rust 同时处理多个异步任务
java·数据库·rust
杰克逊的黑豹20 小时前
不再迷茫:Rust, Zig, Go 和 C
c++·rust·go
Source.Liu21 小时前
【学Rust写CAD】28 带 Alpha 通道的双线性插值函数(bilinear_interpolation_alpha.rs)
rust
Source.Liu21 小时前
【学Rust写CAD】27 双线性插值函数(bilinear_interpolation.rs)
后端·rust·cad
yinhezhanshen21 小时前
理解rust里面的copy和clone
开发语言·后端·rust
叠叠乐1 天前
rust Send Sync 以及对象安全和对象不安全
开发语言·安全·rust
ALe要立志成为web糕手1 天前
SESSION_UPLOAD_PROGRESS 的利用
python·web安全·网络安全·ctf
niandb1 天前
The Rust Programming Language 学习 (九)
windows·rust