使用lazy_static时遇到[E0195]生命周期参数或类型“Target”的边界与特征声明不匹配

问题描述 投票:0回答:1

在做

cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0'
时,我遇到了以下问题:

error[E0195]: lifetime parameters or bounds on type `Target` do not match the trait declaration
  --> C:\Users\USER\.cargo\registry\src\index.crates.io-6f17d22bba15001f\console-0.15.7\src\utils.rs:25:1
   |
25 | / lazy_static! {
26 | |     static ref STDOUT_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stdout()));
27 | |     static ref STDERR_COLORS: AtomicBool = AtomicBool::new(default_colors_enabled(&Term::stderr()));
28 | | }
   | |_^ lifetimes do not match type in trait
   |
   = note: this error originates in the macro `__lazy_static_internal` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info)

由于这不是我安装的第一台机器

flutter_rust_bridge_codegen
,我很确定这不是版本的问题。

我怀疑

lazy_static
板条箱的缓存版本存在问题。

rust rust-cargo
1个回答
0
投票

我深入缓存

%USERPROFILE%\.cargo\registry\src\index.crates.io-6f17d22bba15001f\lazy_static-1.4.0\src\lib.rs
并找到了定义
type Target

的行

我的缓存版本是

type Target<'a> = $T;
我修改为
type Target = $T;

然后我重新运行

cargo install 'flutter_rust_bridge_codegen@^2.0.0-dev.0'

问题解决了

进一步说明: 我在自己的开发中也遇到过类似的问题,并且完全避免使用

lazy_static
,因为我认为我对
lazy_static
用法的理解是错误的。

这一次,我意识到问题不仅仅发生在我的代码上。

我的“修复”可能并不完全正确。但对我来说,这个

<'a>
似乎没有必要,而且处理起来真的很痛苦。但至少它可以让我专注于我更关心的事情。

我在SO中找不到类似的案例。所以我的假设是这个lazy_static问题可能是由我自己的无知引起的。我很想知道我可以做得更好。

© www.soinside.com 2019 - 2024. All rights reserved.