Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust:错误[E0495]:由于闭包中的要求冲突,无法推断自动引用的适当生存期 原始想法_Rust_Closures_Lifetime - Fatal编程技术网

Rust:错误[E0495]:由于闭包中的要求冲突,无法推断自动引用的适当生存期 原始想法

Rust:错误[E0495]:由于闭包中的要求冲突,无法推断自动引用的适当生存期 原始想法,rust,closures,lifetime,Rust,Closures,Lifetime,在一个虚拟项目中,我想使用循环迭代器(例如生成整数) 使用std::iter::Cycle; 类型IntegerCycle=Cycle>; fn生成_循环()->[整数循环;2]{ 设mut循环=[ [1, 2], [2, 4], ]; 周期 .地图(|点|{ 点。地图(|点|点*2) }) .地图(|点|{ points.iter().cycle() }) } fn main(){ 让mut cycles=generate_cycles(); // ... } 问题 上面的解决方案不起作用,

在一个虚拟项目中,我想使用循环迭代器(例如生成整数)

使用std::iter::Cycle;
类型IntegerCycle=Cycle>;
fn生成_循环()->[整数循环;2]{
设mut循环=[
[1, 2],
[2, 4],
];
周期
.地图(|点|{
点。地图(|点|点*2)
})
.地图(|点|{
points.iter().cycle()
})
}
fn main(){
让mut cycles=generate_cycles();
// ...
}
问题 上面的解决方案不起作用,而且,作为一个最近接触到“生命周期”概念的生锈初学者,我不明白为什么编译器在这里抱怨,或者我能做些什么让他高兴

error[E0495]: cannot infer an appropriate lifetime for autorefdue to conflicting requirements
  --> src/main.rs:20:14
   |
20 |       points.iter().cycle()
   |              ^^^^
   |
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 19:10...
  --> src/main.rs:19:10
   |
19 |       .map(|points| {
   |  __________^
20 | |       points.iter().cycle()
21 | |     })
   | |_____^
note: ...so that reference does not outlive borrowed content
  --> src/main.rs:20:7
   |
20 |       points.iter().cycle()
   |       ^^^^^^
   = note: but, the lifetime must be valid for the static lifetime...
   = note: ...so that the expression is assignable:
           expected [std::iter::Cycle<std::slice::Iter<'static, i32>>; 2]
              found [std::iter::Cycle<std::slice::Iter<'_, i32>>; 2]
错误[E0495]:由于需求冲突,无法推断自动引用的适当生存期
-->src/main.rs:20:14
|
20 |点.iter().循环()
|              ^^^^
|
注意:首先,生命周期不能超过19:10在身体上定义的匿名生命周期#2。。。
-->src/main.rs:19:10
|
19.地图(点){
|  __________^
20 | |点.iter().循环()
21 | |     })
| |_____^
注意:…这样引用就不会超过借用的内容
-->src/main.rs:20:7
|
20 |点.iter().循环()
|       ^^^^^^
=注意:但是,生存期必须对静态生存期有效。。。
=注意:…因此表达式是可赋值的:
预期[std::iter::Cycle>;2]

下面是一个REPL,代码试图利用类型声明中的
数组映射:。

type IntegerCycle = Cycle<std::slice::Iter<'static, i32>>;
但是,您可以尝试一个类似于以下简单代码的代码:

let a = [1, 2].map(|x| 2 * x);
let r: &'static [i32; 2] = &a; //error: borrowed value does not live long enough
这是
arraymap::map
的结果,它是一个普通数组,而不是一个文本数组,因此它没有
的静态
生存期。它不能是静态的,因为您正在运行时计算值。在我的例子中,只要变量
a
,它就会一直存在

在您的例子中,由于
arraymap::map
的返回值没有分配给变量,因此它们是临时值,很快就会被删除。但是,即使将其分配给局部变量,也无法返回对它的引用,因为当函数结束时,局部变量将被删除

解决方案是返回一个拥有该值的迭代器。类似这样的工作原理:

type IntegerCycle = Cycle<std::vec::IntoIter<i32>>;

fn generate_cycles() -> [IntegerCycle; 2] {
  let cycles = [
    [1, 2],
    [2, 4],
  ];
  cycles
    .map(|points| {
      points.map(|point| point*2)
    })
    .map(|points| {
      points.to_vec().into_iter().cycle()
    })
}
注意:似乎有人试图向
std
添加一个适当的文件,但仍有一些未决问题

type IntegerCycle = Cycle<std::vec::IntoIter<i32>>;

fn generate_cycles() -> [IntegerCycle; 2] {
  let cycles = [
    [1, 2],
    [2, 4],
  ];
  cycles
    .map(|points| {
      points.map(|point| point*2)
    })
    .map(|points| {
      points.to_vec().into_iter().cycle()
    })
}
type IntegerCycle = Cycle<arrayvec::IntoIter<[i32; 2]>>;

fn generate_cycles() -> [IntegerCycle; 2] {
  let cycles = [
    [1, 2],
    [2, 4],
  ];

  cycles
    .map(|points| {
      points.map(|point| point*2)
    })
    .map(|points| {
        let a = arrayvec::ArrayVec::from(*points);
        a.into_iter().cycle()
    })
}