小编Pet*_*all的帖子

为什么通过移动捕获弧会使我的关闭 FnOnce 而不是 Fn

在下面的示例中,我使用 Arc 从请求处理程序引用服务器状态,但编译器将闭包设为FnOnce. 感觉就像我在做正确的事情,因为每个闭包都拥有对状态的强引用。为什么这不起作用?有什么选择可以使它工作?闭包之间的共享弧等其他问题表明类似这样的工作,但我正在制作每个闭包的克隆,如图所示,但仍然出现错误。

#![feature(async_closure)]

#[derive(Default, Debug)]
struct State {}

impl State {
    pub async fn exists(&self, key: &str) -> bool {
        true
    }

    pub async fn update(&self, key: &str) {}
}

#[tokio::main]
async fn main() {
    use warp::Filter;
    use std::sync::Arc;

    let state: Arc<State> = Arc::default();

    let api = warp::post()
        .and(warp::path("/api"))
        .and(warp::path::param::<String>().and_then({
            let state = Arc::clone(&state);
            async move |p: String| {
                let x = state.exists(&p);
                if x.await {
                    Ok(p)
                } else {
                    Err(warp::reject::not_found())
                }
            }
        })) …
Run Code Online (Sandbox Code Playgroud)

closures rust

10
推荐指数
1
解决办法
307
查看次数

RankNTypes:导致此错误的原因是什么?

我一直在探索Rank2Types和RankNTypes以尝试熟悉它们.但我无法弄清楚为什么以下不起作用.

g :: (forall a. forall b. a -> b) -> x -> y -> (u,v)
g p x y = (p x, p y)
Run Code Online (Sandbox Code Playgroud)

编译器接受此定义,但在尝试使用它时失败:

ghci> g id 1 2

<interactive>:35:3:
    Couldn't match type `a' with `b'
      `a' is a rigid type variable bound by
          a type expected by the context: a -> b at <interactive>:35:1
      `b' is a rigid type variable bound by
          a type expected by the context: a -> b at <interactive>:35:1
    Expected type: a -> …
Run Code Online (Sandbox Code Playgroud)

haskell types ghc

9
推荐指数
1
解决办法
223
查看次数

在Haskell中更正if语句的语法

您需要的唯一输入是您获得的成绩编号.这就是我到目前为止所拥有的.

myScore x = if x > 90
    then let x = "You got a A"
if 80 < x < 90 
    then let x = "you got a B"
if 70 < x < 80
    then let x = "You got a C"
if 60 < x < 90
    then let x = "you got a D"
else let x = "You got a F"
Run Code Online (Sandbox Code Playgroud)

这给了我一个错误"解析错误输入`if'",我也尝试过:

myScore x = (if x > 90 then "You got an A" | …
Run Code Online (Sandbox Code Playgroud)

haskell if-statement

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

如何使用输入列表列出AD用户的AD组成员资格?

我是相当新的PS用户...寻找PowerShell脚本的一些帮助,以获取用户所属的安全组列表.

描述我需要的东西:

  • 我有许多用户的输入列表(txt文件)(samaccountnames).每个名字都在新的一行上.
  • 我需要脚本在AD中搜索这些名称 - 整个林,而不仅仅是一个域
  • 输出应该看起来像"samaccountname"和这个帐户在一行中成员的组列表,所以我可以在excel中对它进行排序

这是我的脚本:

$users = Get-Content C:\users.txt

ForEach ($User in $users) {
  $getmembership = Get-ADUser $User.Users -Properties MemberOf | Select -ExpandProperty memberof
  $getmembership | Out-File -Append c:\membership.txt 
}
Run Code Online (Sandbox Code Playgroud)

但它给我一个错误:

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again.
At line:4 char:28
+ $getmembership = Get-ADUser <<<<  $User.Users -Properties MemberOf | Select -ExpandProperty memberof
    + CategoryInfo          : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : …
Run Code Online (Sandbox Code Playgroud)

membership powershell active-directory

9
推荐指数
2
解决办法
13万
查看次数

如何将代码重写为新的未装箱的闭包

有人可以帮我用新的无盒装闭包重写这段代码:

struct Builder;
pub fn build(rules: |params: &mut Builder|) -> Builder {
    let mut builder = Builder::new();
    rules(&mut builder);

    builder
}
Run Code Online (Sandbox Code Playgroud)

我试着像这样写,但我得到了一生的错误:

pub fn build<F>(rules: F) -> Builder where F: FnOnce<(&mut Builder,), ()> {
    let mut builder = Builder::new();
    rules(&mut builder);

    builder
}

valico/src/builder.rs:48:59: 48:71 error: missing lifetime specifier [E0106]
valico/src/builder.rs:48     pub fn build<F>(rules: F) -> Builder where F: FnOnce<(&mut Builder,), ()> {
                                                                                   ^~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我需要指定什么生命周期?沙盒中的简化示例.

rust rust-obsolete

9
推荐指数
1
解决办法
587
查看次数

从父模块导入宏

我在箱子里重复使用宏时遇到了麻烦.

如果在以下位置定义了宏./src/macros.rs:

#[macro_export]
macro_rules! my_macro {
    ...
}
Run Code Online (Sandbox Code Playgroud)

用于./src/lib.rs:

#[macro_use]
pub mod macros;
Run Code Online (Sandbox Code Playgroud)

我看不到这个宏./src/submod/lib.rs:

my_macro!(...);
Run Code Online (Sandbox Code Playgroud)

它会产生错误消息error: macro undefined: 'my_macro!'.

有没有办法在这个子模块中导入这个宏submod

rust rust-macros rust-decl-macros

9
推荐指数
1
解决办法
1464
查看次数

什么是< - Rust中的符号?

<-Rust中的运算符/表达式是什么?你可以在这里找到符号.

我碰巧在查看描述Rust中表达式和操作的页面.我没有在Rust中编程,所以我问了一个朋友谁是亲Rust这个符号是什么,但即使他不知道它是什么.

syntax operators placement-new rust

9
推荐指数
1
解决办法
601
查看次数

自动解除引用和deref强制之间有什么关系?

经过一些讨论,我现在有点困惑之间的关系auto-dereferencingderef coercion.

似乎 "自动解除引用"一词仅适用于取消引用的目标是方法接收者,而"deref强制"一词似乎适用于函数参数及其所需的所有上下文.

我认为解除引用并不总是涉及deref强制,但我不确定:dereferencing是否始终使用某些Deref::deref特征实现?

如果是这样,是T: Deref<Target = U> where T: &U编译器内置的实现者吗?

最后,在编译器隐式转换&&&&x为的所有情况下,使用术语"autoderef"听起来很自然&x:

pub fn foo(_v: &str) -> bool {
    false
}

let x="hello world";
foo(&&&&x);
Run Code Online (Sandbox Code Playgroud)

这是社区的普遍共识吗?

terminology dereference rust

9
推荐指数
1
解决办法
276
查看次数

使用 rusoto 将字符串上传到 S3

我正在使用rusoto S3 创建一个 JSON 字符串并将此字符串上传到 S3 存储桶。我可以创建字符串,但 rusoto 的 S3PutObjectRequest需要 a StreamingBody,我不确定如何StreamingBody从字符串创建 a ,或者这是否真的有必要。

extern crate json;
extern crate rusoto_core;
extern crate rusoto_s3;
extern crate futures;

use rusoto_core::Region;
use rusoto_s3::{S3, S3Client, PutObjectRequest};

fn main() {
    let mut paths = Vec::new();
    paths.push(1);
    let s3_client = S3Client::new(Region::UsEast1);
    println!("{}", json::stringify(paths));
    s3_client.put_object(PutObjectRequest {
        bucket: String::from("bucket"),
        key: "@types.json".to_string(),
        body: Some(json::stringify(paths)),
        acl: Some("public-read".to_string()),
        ..Default::default()
    }).sync().expect("could not upload");
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是

extern crate json;
extern crate rusoto_core;
extern crate rusoto_s3;
extern crate …
Run Code Online (Sandbox Code Playgroud)

type-conversion amazon-s3 rust rusoto

9
推荐指数
1
解决办法
2433
查看次数

在 Rust 中,你能拥有一个字符串字面量吗?

根据The Rust 的书

Rust 中的每个值都有一个变量,称为其所有者。一次只能有一个所有者。当所有者超出范围时,该值将被删除。

根据rust-lang.org

静态项目不会在程序结束时调用 drop。

在阅读了这篇 SO post并给出了下面的代码后,我明白这foo是一个值,其变量y相当于&y因为“字符串文字是字符串切片”,被称为它的owner. 那是对的吗?还是静态项目没有所有者?

let x = String::from("foo");  // heap allocated, mutable, owned
let y = "foo" // statically allocated to rust executable, immutable
Run Code Online (Sandbox Code Playgroud)

我想知道,因为与拥有的不同String,字符串文字没有移动,大概是因为它们存储在.rodata可执行文件中

fn main() {
  let s1 = "foo"; // as opposed to String::from("foo")
  let s2 = s1; // not moved
  let s3 = s2; // no …
Run Code Online (Sandbox Code Playgroud)

string reference string-literals ownership rust

9
推荐指数
1
解决办法
1764
查看次数