← 返回卡片

原书出处

Write Powerful Rust Macros

07 From panic to result: Error handling 传播或组合诊断

核验版本的摘录快照 · 导入于 2026-11-25T09:20:41.655Z

Another cool Rust feature makes its appearance in the previous code. As you can see, we map over statements and return a Result for each one. That means our output should be Vec<Result<Stmt, syn::Error>>. But the output type I defined for new_statements in handle_expression is Result<Vec<Stmt>, syn::Error>! In the functional world, this is called traverse, which allows stacking your enums (actually, monads, but let’s not get into that) in different ways.

The reason this works in Rust is

此预览只包含定位所需的摘录和上下文。

原书定位详情

来源:a19c7933-4eda-4176-aebd-30fe7e1ad292

正文位置:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-7.xhtml#p151

EPUB SHA-256:68c7ed23324b44144895c3bb55f55731454da237867b66ec66ca6404f5b93661