在下面的示例中,我使用 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) 我一直在探索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) 您需要的唯一输入是您获得的成绩编号.这就是我到目前为止所拥有的.
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) 我是相当新的PS用户...寻找PowerShell脚本的一些帮助,以获取用户所属的安全组列表.
描述我需要的东西:
这是我的脚本:
$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) 有人可以帮我用新的无盒装闭包重写这段代码:
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)
我需要指定什么生命周期?沙盒中的简化示例.
我在箱子里重复使用宏时遇到了麻烦.
如果在以下位置定义了宏./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
?
经过一些讨论,我现在有点困惑之间的关系auto-dereferencing
和deref 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)
这是社区的普遍共识吗?
我正在使用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) 根据The Rust 的书:
Rust 中的每个值都有一个变量,称为其所有者。一次只能有一个所有者。当所有者超出范围时,该值将被删除。
静态项目不会在程序结束时调用 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) rust ×7
haskell ×2
amazon-s3 ×1
closures ×1
dereference ×1
ghc ×1
if-statement ×1
membership ×1
operators ×1
ownership ×1
powershell ×1
reference ×1
rusoto ×1
rust-macros ×1
string ×1
syntax ×1
terminology ×1
types ×1