相关疑难解决方法(0)

返回迭代器(或任何其他特征)的正确方法是什么?

以下Rust代码编译并运行没有任何问题.

fn main() {
    let text = "abc";
    println!("{}", text.split(' ').take(2).count());
}
Run Code Online (Sandbox Code Playgroud)

在那之后,我尝试了类似的东西....但它没有编译

fn main() {
    let text = "word1 word2 word3";
    println!("{}", to_words(text).take(2).count());
}

fn to_words(text: &str) -> &Iterator<Item = &str> {
    &(text.split(' '))
}
Run Code Online (Sandbox Code Playgroud)

主要问题是我不确定函数to_words()应该具有什么返回类型.编译器说:

error[E0599]: no method named `count` found for type `std::iter::Take<std::iter::Iterator<Item=&str>>` in the current scope
 --> src/main.rs:3:43
  |
3 |     println!("{}", to_words(text).take(2).count());
  |                                           ^^^^^
  |
  = note: the method `count` exists but the following trait bounds were not satisfied:
          `std::iter::Iterator<Item=&str> : std::marker::Sized`
          `std::iter::Take<std::iter::Iterator<Item=&str>> …
Run Code Online (Sandbox Code Playgroud)

rust

92
推荐指数
1
解决办法
2万
查看次数

从函数返回闭包

注意:这个问题是在Rust第一次稳定发布之前提出的.之后发生了很多变化,函数中使用的语法甚至不再有效.尽管如此,Shepmaster的答案仍然非常出色,这使得这个问题值得保留.


最后,未装箱的封闭装置着陆了,所以我正在试验它们,看看你能做些什么.

我有这个简单的功能:

fn make_adder(a: int, b: int) -> || -> int {
    || a + b
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到了一个missing lifetime specifier [E0106]错误.我试图通过将返回类型更改为修复此问题||: 'static -> int,但后来又出现了另一个错误cannot infer an appropriate lifetime due to conflicting requirements.

如果我理解正确,关闭是未装箱的,所以它拥有ab.我觉得它需要一辈子似乎很奇怪.我怎样才能解决这个问题?

rust

32
推荐指数
3
解决办法
1万
查看次数

为什么需要使用plus运算符(Iterator <Item =&Foo> +'a)为特征添加生命周期?

我在迭代器上应用了一个闭包,我想使用stable,所以我想返回一个盒装Iterator.这样做的显而易见的方法如下:

struct Foo;

fn into_iterator(myvec: &Vec<Foo>) -> Box<dyn Iterator<Item = &Foo>> {
    Box::new(myvec.iter())
}
Run Code Online (Sandbox Code Playgroud)

这是失败的,因为借用检查器无法推断出适当的生命周期.

经过一番研究,我找到了正确的方法来返回迭代器?,这让我想补充一下+ 'a:

fn into_iterator<'a>(myvec: &'a Vec<Foo>) -> Box<dyn Iterator<Item = &'a Foo> + 'a> {
    Box::new(myvec.iter())
}
Run Code Online (Sandbox Code Playgroud)

但我不明白

  • 这是做什么的
  • 为什么需要这里呢

lifetime rust

13
推荐指数
1
解决办法
761
查看次数

有条件地迭代几个可能的迭代器之一

我正在尝试根据Option函数的输入切换行为.想法是基于给定Option是否存在来迭代.这是一个最小的,如果愚蠢的例子:

use std::iter;

fn main() {
    let x: Option<i64> = None;

    // Repeat x 5 times if present, otherwise count from 1 to 5
    for i in match x {
        None => 1..5,
        Some(x) => iter::repeat(x).take(5),
    } {
        println!("{}", i);
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

error[E0308]: match arms have incompatible types
  --> src/main.rs:7:14
   |
7  |       for i in match x {
   |  ______________^
8  | |         None => 1..5,
9  | |         Some(x) => iter::repeat(x).take(5),
   | | …
Run Code Online (Sandbox Code Playgroud)

rust

9
推荐指数
4
解决办法
1407
查看次数

在盒装未来中使用引用变量时“需要显式生命周期”

我正在尝试使用创建的结构main()并将其传递给返回盒装Future. 然而,我遇到了终身和借贷问题,似乎无法彻底解决这个问题。

这是我的结构和函数:

extern crate futures; // 0.1.21
extern crate tokio_core; // 0.1.17

use futures::{future::ok, Future};

pub struct SomeStruct {
    some_val: u32,
}

impl SomeStruct {
    pub fn do_something(&self, value: u32) -> u32 {
        // Do some work
        return self.some_val + value;
    }
}

fn main() {
    let core = tokio_core::reactor::Core::new().unwrap();
    let my_struct = SomeStruct { some_val: 10 };

    let future = get_future(&my_struct);
    core.run(future);

    let future2 = get_future(&my_struct);
    core.run(future2);
}

fn get_future(some_struct: &SomeStruct) -> Box<Future<Item = …
Run Code Online (Sandbox Code Playgroud)

future lifetime rust borrowing rust-tokio

3
推荐指数
1
解决办法
7264
查看次数

标签 统计

rust ×5

lifetime ×2

borrowing ×1

future ×1

rust-tokio ×1