Rust in Everything
基于 async-stream 0.3.6

常见坑

每个坑先用最小程序复现(报错是 M2 Max、rustc 1.99.0 上的原文,长报错只摘开头几行,本机路径前缀已省略),再说它在宏展开或运行时的哪一步产生,最后给出改法并跑一遍。机制在 01、02 讲过的,这里只链过去。

  1. 编译报错:四条报错,都和宏改写出来的代码有关。
    • {async block@...} cannot be unpinned(E0277):对 stream! 的返回值直接 .next()。
    • yield syntax is experimental(E0658):yield 写在闭包、async 块或嵌套 fn 里,Scrub 不改写,原样留给编译器。
    • use of unreachable label `'__async_stream_private_check_scope` (E0767):yield 经由别的宏(比如 tokio::join!)落进了 async 块,#106 的防线在报错。
    • type annotations needed for AsyncStream<Result<i32, _>, ...>(E0282):? 改写里的 e.into() 推不出错误类型。
  2. ? 之后流就结束了:两个宏里的 ? 是同一种改写。
    • expected `Result<_, _>`, found `i32` (E0308):stream! 里写了 ?,Item 就必须是 Result。
    • 第一个 Err 之后流就结束了:? 发出 Err 后是 return,不是 continue;想跳过错误接着处理,就自己 match。
flowchart TB
  q{碰到了什么}
  q -->|"cannot be unpinned"| p1["1 忘了 pin"]
  q -->|"yield syntax is experimental"| p2["1 yield 不在字面位置"]
  q -->|"unreachable label"| p3["1 别的宏把 yield 包进 async 块"]
  q -->|"type annotations needed"| p4["1 错误类型推不出"]
  q -->|"expected Result, found ..."| p5["2 stream! 里的 ?"]
  q -->|"流提前结束、后面的项没了"| p6["2 ? 之后是 return"]

💬 关联讨论

发起讨论
加载中...