我可以在Rust中传递函数作为参数(可能是的),如果可以的话,我可以这么做.
如果你不能,这是一个很好的选择.
我尝试了一些语法,但我没有得到
我知道我能做到这一点
fn example() {
let fun: fn(value: i32) -> i32;
fun = fun_test;
fun(5i32);
}
fn fun_test(value: i32) -> i32 {
println!("{}", value);
value
}
Run Code Online (Sandbox Code Playgroud)
但不是将函数作为参数传递给另一个函数
fn fun_test(value: i32, (some_function_prototype)) -> i32 {
println!("{}", value);
value
}
Run Code Online (Sandbox Code Playgroud) 让我们考虑以下示例:
主文件
use futures::executor::block_on;
use futures::future::{FutureExt, TryFutureExt};
async fn fut1() -> Result<String, u32> {
Ok("ok".to_string())
}
fn main() {
println!("Hello, world!");
match block_on(fut1().and_then(|x| async move { Ok(format!("{} is \"ok\"", x)) })) {
Ok(s) => println!("{}", s),
Err(u) => println!("{}", u)
};
}
Run Code Online (Sandbox Code Playgroud)
Cargo.toml
[dependencies]
futures = "^0.3"
Run Code Online (Sandbox Code Playgroud)
我问的是表达式|x| async move {}而不是async move |x| {}. 后者更明显,但遇到编译错误:
error[E0658]: async closures are unstable
Run Code Online (Sandbox Code Playgroud)
然后我想知道,async move || {}和之间有什么区别|| async move {}。它们似乎都是使用move关键字的闭包。
$ rustc --version …Run Code Online (Sandbox Code Playgroud) 在像C#这样的语言中,给出这个代码(我没有await故意使用关键字):
async Task Foo()
{
var task = LongRunningOperationAsync();
// Some other non-related operation
AnotherOperation();
result = task.Result;
}
Run Code Online (Sandbox Code Playgroud)
在第一行中,long操作在另一个线程中运行,并Task返回a(即未来).然后,您可以执行另一个与第一个并行运行的操作,最后,您可以等待操作完成.我认为,这也是行为async/ await在Python,JavaScript等
另一方面,在Rust中,我在RFC中读到:
Rust的期货与其他语言的期货之间的根本区别在于,除非进行调查,否则Rust的期货不会做任何事情.整个系统是围绕这个建立的:例如,取消正在降低未来正是出于这个原因.相比之下,在其他语言中,调用异步fn会旋转一个立即开始执行的未来.
在这种情况下,是什么目的async/ await鲁斯特?看到其他语言,这种表示法是一种运行并行操作的便捷方式,但是如果调用async函数没有运行任何东西,我无法看到它在Rust中是如何工作的.
我应该使用什么类型的向量来存储 future?
我尝试在同一个 URL 上发出多个并发请求,并将所有 future 保存到向量中以与join_all.
如果我没有明确设置向量的类型,则一切正常。我知道 Rust 可以找到变量的正确类型。CLion 确定向量类型为Vec<dyn Future<Output = ()>>,但是当我尝试自己设置类型时,它给了我一个错误:
error[E0277]: the size for values of type `dyn core::future::future::Future<Output = ()>` cannot be known at compilation time
--> src/lib.rs:15:23
|
15 | let mut requests: Vec<dyn Future<Output = ()>> = Vec::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn core::future::future::Future<Output = ()>`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用新的async/await语法、std::future::Futures 和 Tokio 的最新版本来实现这一点。我正在使用 Tokio0.2.0-alpha.4和 Rust 1.39.0-nightly。
我尝试过的不同的事情包括:
Box<dyn>s 用于我想存储在结构体中的类型我无法得到一个最小的工作版本,所以这是我想要实现的简化版本:
async fn foo(x: u8) -> u8 {
2 * x
}
// type StorableAsyncFn = Fn(u8) -> dyn Future<Output = u8>;
struct S {
f: StorableAsyncFn,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let s = S { f: foo };
let out = (s.f)(1).await;
Ok(())
}
Run Code Online (Sandbox Code Playgroud)
当然,此代码无法编译并出现以下错误:
error[E0412]: cannot …Run Code Online (Sandbox Code Playgroud) 我习惯了 Scala 的类型,在这种类型Future中,您可以包装要返回的任何对象来指定它。Future[..]
我的 Rust 函数hello返回Query,但我似乎无法将该结果作为 type 的参数传递Future<Output = Query>。为什么不呢?我应该如何更好地输入它?
当我尝试将未来作为参数传递时,就会发生失败:
use std::future::Future;
struct Person;
struct DatabaseError;
type Query = Result<Vec<Person>, DatabaseError>;
async fn hello_future(future: &dyn Future<Output = Query>) -> bool {
future.await.is_ok()
}
async fn hello() -> Query {
unimplemented!()
}
async fn example() {
let f = hello();
hello_future(&f);
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
编译失败并出现以下错误:
error[E0277]: `&dyn Future<Output = Result<Vec<Person>, DatabaseError>>` is not a future
--> src/main.rs:9:5
|
9 | …Run Code Online (Sandbox Code Playgroud)