小编har*_*mic的帖子

'&&x' 模式匹配是否会导致 x 被复制?

std::iter::Iterator::filter()的文档中,它解释了值是通过引用传递给闭包的,并且由于许多迭代器产生引用,在这种情况下传递的值是对引用的引用。它提供了一些改进人体工程学的建议,方法是使用一种&x模式来删除一个间接级别,或者使用一种&&x模式来删除两个间接级别。

但是,我发现如果被迭代的项目没有实现,则第二个模式不会编译Copy

#[derive(PartialEq)]
struct Foo(i32);

fn main() {
    let a = [Foo(0), Foo(1), Foo(2)];

    // This works
    let _ = a.iter().filter(|&x| *x != Foo(1));

    // This also works
    let _ = a.iter().filter(|&x| x != &Foo(1));

    // This does not compile
    let _ = a.iter().filter(|&&x| x != Foo(1));
}
Run Code Online (Sandbox Code Playgroud)

你得到的错误是:

error[E0507]: cannot move out of a shared reference
  --> src/main.rs:14:30
   |
14 |     let _ = a.iter().filter(|&&x| x != Foo(1));
   |                              ^^-
   | …
Run Code Online (Sandbox Code Playgroud)

reference pattern-matching rust

4
推荐指数
1
解决办法
119
查看次数

无法移动而无法从函数返回std :: unique_ptr

我正在更新一些auto_ptr用来unique_ptr代替的旧代码。它主要是一个搜索和替换工作,但是我发现我在代码返回a的一个地方遇到了编译错误unique_ptr

这是说明问题的示例:

#include <string>
#include <iostream>
#include <memory>
#include <utility>

using namespace std;

struct Foo {
    unique_ptr<string> us;

    Foo() {
        this->us = unique_ptr<string>(new string("hello"));
    }

    unique_ptr<string> bar() {
        return this->us;
    }

};


int main(int argc, const char **argv) {

    Foo foo;
    unique_ptr<string> s = foo.bar();
    cout << *s << endl;

}
Run Code Online (Sandbox Code Playgroud)

当我编译它时,我得到了:

t1.cpp: In member function ‘std::unique_ptr<std::basic_string<char> > Foo::bar()’:
t1.cpp:17:16: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = std::basic_string<char>; _Dp …
Run Code Online (Sandbox Code Playgroud)

c++ unique-ptr

3
推荐指数
2
解决办法
149
查看次数

如何调用为 &amp;str 实现的关联函数?

我正在尝试编写一个自定义Deserialize实现,它可以对我的枚举执行不区分大小写的反序列化:

use serde::{Deserialize, Deserializer}; // 1.0.120
use serde_json; // 1.0.61

#[derive(Debug, PartialEq)]
enum MyEnum {
    Foo,
    Bar,
}

// Implements case-insensitive deserialization of node types using from_str above
impl<'de> Deserialize<'de> for MyEnum {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let v = String::deserialize(deserializer)?;
        match v.to_ascii_lowercase().as_ref() {
            "foo" => Ok(MyEnum::Foo),
            "bar" => Ok(MyEnum::Bar),
            _ => Err(serde::de::Error::custom("invalid value")),
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这有效,但会导致两种分配:一种是将输入值放入 a 中String,另一种是将其转换String为大写。我不需要第一个,因为 serde 提供了Deserialize for &str. 我怎么称呼它?

这不起作用: …

rust serde

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

用取消方法增加ES6承诺

我正在尝试编写一些代码,这些代码在启动一些可能长时间运行的异步活动后返回ES6承诺.但是,我希望有可能取消该活动,所以我想用'取消'方法来增加我的承诺.

一个sscce说明了我想要做的是如下:

function TimerPromise(timeInterval) {
    var timer;

    var p = new Promise(
        function(resolve,reject) {
            timer = setTimeout(
                function() {
                    resolve(true);
                },
                timeInterval
            );
        }
    );

    p.cancel = function() {
        clearTimeout(timer);
    };

    console.log("p.cancel is ",p.cancel);

    return p;
}

var t = TimerPromise(2000).then(function(res) { console.log("Result is ",res); });

t.cancel();
Run Code Online (Sandbox Code Playgroud)

在示例中,TimerPromise只设置一个计时器来模拟长时间运行的异步活动.

这是我在运行时得到的:

$ node test.js
p.cancel is  function () {
                timer.clearTimeout();
        }
/home/harmic/js/src/test.js:28
t.cancel();
  ^

TypeError: t.cancel is not a function
    at Object.<anonymous> (/home/harmic/js/src/test.js:28:3)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise cancellation es6-promise

0
推荐指数
1
解决办法
399
查看次数