简体   繁体   English

为什么这个迭代器需要一个生命周期,我该如何给它一个生命周期?

[英]Why does this iterator require a lifetime, and how do I give it one?

I'm trying to get the following to compile:我正在尝试编译以下内容:

fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
  tokens.map(|t| t.to_owned())
}
error[E0482]: lifetime of return value does not outlive the function call
 --> src/lib.rs:1:53
  |
1 | fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
  |                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the return value is only valid for the lifetime `'a` as defined on the function body at 1:9
 --> src/lib.rs:1:9
  |
1 | fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
  |         ^^

Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=55460a8b17ff10e30b56a9142338ec19游乐场链接: https : //play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=55460a8b17ff10e30b56a9142338ec19

What's strange to me is that the return value is concerned with lifetimes at all;令我感到奇怪的是,返回值根本与生命周期有关; its contents are owned and passed directly outward.它的内容被拥有并直接向外传递。

I came across this blog post:https://blog.katona.me/2019/12/29/Rust-Lifetimes-and-Iterators/我看到了这篇博文:https ://blog.katona.me/2019/12/29/Rust-Lifetimes-and-Iterators/

but neither of these worked:但这些都没有奏效:

fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> + '_ {
fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> + 'a {

I suspect I'm misunderstanding some key aspect of Iterators here.我怀疑我在这里误解了迭代器的一些关键方面。 Any suggestions?有什么建议?

I solved it by adding + 'a to both Iterators:我通过向两个迭代器添加+ 'a来解决它:

fn read<'a>(tokens: impl Iterator<Item=&'a str> + 'a) -> impl Iterator<Item=String> + 'a {
  tokens.map(|t| t.to_owned())
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我需要为我的迭代器项提供生命周期参数? - Why do I need to provide a lifetime parameter for my iterator item? 如何指定引用自身但不会自我变异的迭代器的关联类型的生命周期? - How do I specify the lifetime for the associated type of an iterator that refers to itself but does not mutate itself? 为什么 Rust 需要此变量的“静态”生命周期? - Why does Rust require a `'static` lifetime for this variable? 为什么结构中的切片需要一生,但不是矢量? - Why do slices in a structure require a lifetime, but not vectors? 为什么for循环不需要可变迭代器? - Why does a for loop not require a mutable iterator? 为什么 Vec.sort() 似乎需要 static 生命周期? - Why does Vec.sort() seem to require a static lifetime? 为什么我在这里需要'静态生命周期以及如何解决它? - Why do I need 'static lifetime here and how to fix it? 为什么这个 Rust 代码编译时在结构上有生命周期绑定,但如果绑定仅在 impl 上,则会给出生命周期错误? - Why does this Rust code compile with a lifetime bound on the struct, but give a lifetime error if the bound is only on the impl? 如何在返回新接收的值和缓存值之间的所有组合的迭代器时修复生命周期问题? - How do I fix a lifetime issue when returning an iterator of all combinations between a newly received value and cached values? 为什么`Iterator.find()`需要一个可变的`self`引用? - Why does `Iterator.find()` require a mutable `self` reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM