简介
在本实验中,我们将学习 Rust 中的 panic!
宏,它可用于引发恐慌并开始展开堆栈,导致程序报告恐慌消息并退出。运行时会通过调用对象的析构函数来释放线程拥有的所有资源。我们还将查看一个使用 panic!
宏处理除零错误的示例,并使用 Valgrind 验证它不会导致内存泄漏。
注意:如果实验未指定文件名,你可以使用任何你想要的文件名。例如,你可以使用
main.rs
,并通过rustc main.rs &&./main
进行编译和运行。
Skills Graph
%%%%{init: {'theme':'neutral'}}%%%%
flowchart RL
rust(("Rust")) -.-> rust/DataStructuresandEnumsGroup(["Data Structures and Enums"])
rust(("Rust")) -.-> rust/ErrorHandlingandDebuggingGroup(["Error Handling and Debugging"])
rust(("Rust")) -.-> rust/BasicConceptsGroup(["Basic Concepts"])
rust(("Rust")) -.-> rust/FunctionsandClosuresGroup(["Functions and Closures"])
rust(("Rust")) -.-> rust/MemorySafetyandManagementGroup(["Memory Safety and Management"])
rust(("Rust")) -.-> rust/AdvancedTopicsGroup(["Advanced Topics"])
rust(("Rust")) -.-> rust/DataTypesGroup(["Data Types"])
rust/BasicConceptsGroup -.-> rust/variable_declarations("Variable Declarations")
rust/DataTypesGroup -.-> rust/integer_types("Integer Types")
rust/DataTypesGroup -.-> rust/string_type("String Type")
rust/FunctionsandClosuresGroup -.-> rust/function_syntax("Function Syntax")
rust/FunctionsandClosuresGroup -.-> rust/expressions_statements("Expressions and Statements")
rust/MemorySafetyandManagementGroup -.-> rust/lifetime_specifiers("Lifetime Specifiers")
rust/DataStructuresandEnumsGroup -.-> rust/method_syntax("Method Syntax")
rust/ErrorHandlingandDebuggingGroup -.-> rust/panic_usage("panic! Usage")
rust/AdvancedTopicsGroup -.-> rust/operator_overloading("Traits for Operator Overloading")
subgraph Lab Skills
rust/variable_declarations -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/integer_types -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/string_type -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/function_syntax -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/expressions_statements -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/lifetime_specifiers -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/method_syntax -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/panic_usage -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
rust/operator_overloading -.-> lab-488373{{"Rust 恐慌处理与内存安全"}}
end