如何将“ async fn foo(&[Bar])”与“ task :: spawn(foo)”一起使用?

时间:2019-11-16 13:02:54

标签: rust async-await borrow-checker

我遇到了一些问题,并且想使用task :: spawn(count)而不复制字符串。

async fn count(input: &str) -> Result<HashMap<char, usize>> {
    // ...
}

async fn collect(input: &[&str]) -> Result<HashMap<char, usize>> {
    // ...
    for l in input.iter() {
        // I would like to wirte something like:
        // v.push(Box::pin(task::spawn(count(l))));
        // but the borrow checker is fighting me.
        // v.push(Box::pin(task::spawn(count(l)))) works if
        // I copy with l.to_string() and change the function signature
        // appropriately
        v.push(Box::pin(count(l)));
        // Pin is needed for the select_all(..)
    }
    // ...
}

代码可在此处找到:https://github.com/jk-jonas/parallel-letter-frequency

0 个答案:

没有答案