- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如题。你可以吗?或者它是否需要一个中间步骤来进行特征转换?
我意识到关闭改革还没有完全完成,还有很多 Unresolved 问题。
但是,bug 跟踪器充满了 bug 和 RFC 以及指向过时和新票证的链接,我很难理解新的未装箱的 rust 闭包的运行状态。
这个有效:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test(bar:Box<FnOnce<(int,), ()> + Send>) {
bar.call_once((10,));
}
fn main() {
let bar:Box<FnOnce<(int,), ()> + Send> = box |:x:int| { println!("Hi:{}", x); };
test(bar);
test(box |:x:int| { println!("Hi:{}", x); });
}
这有效:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
let bar2:Box<FnOnce<(int,), ()> + Send> = box |:x:int| { println!("Hi:{}", x); };
test2(Some(bar2));
}
但是,由于某些原因这不起作用:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
test2(Some(box |:x:int| { println!("Hi:{}", x); }));
}
错误:
foo.rs:21:9: 21:53 error: mismatched types: expected `core::option::Option<Box<core::ops::FnOnce<(int,), ()>+Send>>`, found `core::option::Option<Box<closure>>` (expected trait core::ops::FnOnce, found closure)
foo.rs:21 test2(Some(box |:x:int| { println!("Hi:{}", x); }));
那么,问题:
1) 应该上面的代码有效吗?或者 Option<>
/Some()
暗示我在这种情况下还没有理解的复杂事物?
2) 如果是这样,是否有在 |:|
之间进行隐式转换的公开票证糖和FnOnce
类型?
(因为 https://github.com/rust-lang/rust/issues/18101 似乎建议应该这样做,这就是示例 #1 起作用的原因,但我已经完全迷失了试图通过那个元错误来找到任何引用闭包糖的特定错误)。
最佳答案
值得注意的是,这是特征对象和通用结构/枚举的一般属性,完全不特定于未装箱的闭包,FnOnce
, Option
或 |:|
(尤其不是这个,因为它只是创建实现特定特征的值的好方法)。
此外,最近的RFC #401是相关的。
1) Should the above code work? Or does Option<>/Some() imply something complex that I haven't understood in this context?
Box<FnOnce<..>>
是特征对象,因此与非动态 Box<F>
不具有相同的表示或类型对于 F: FnOnce<...>
.从理论上讲,我们可以通过某些类型的枚举和结构支持更高级的深度强制转换,但它不一定能很好地/统一地处理协方差和反方差。
在任何情况下,你都可以做一个普通的特征对象转换:
#![feature(unboxed_closures, unboxed_closure_sugar)]
#![allow(dead_code, unused_variables)]
fn test2(mut bar:Option<Box<FnOnce<(int,), ()> + Send>>) {
bar.take().unwrap().call_once((10,));
}
fn main() {
test2(Some((box |: x| println!("Hi:{}", x)) as Box<FnOnce(int)+Send>));
}
关于方差的说明,请考虑:
enum Option2<T> {
Empty,
One(fn() -> T)
}
也就是说,它要么不存储任何内容,要么存储一个返回 T
的函数。 .投Option2<Box<X>>
是什么意思至 Option2<Box<Trait>>
?不能强行假装一个函数返回 Box<X>
返回 Box<Trait>
类型的值.
2) If so, is there an open ticket for implicit conversions between the |:| sugar and FnOnce types?
|:|
糖是FnOnce
- 实现类型。 FnOnce
是一个特征,强制转换就像任何其他特征对象一样发生。
关于rust - 你能强制 Some(box| :|) to Option<Box<FnOnce<. ..>>> 使用rust 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828582/
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
考虑这样的代码: trait Foo { fn foo(&self); } fn consume_func(b: Box>) { unimplemented!(); } fn prod
我不明白在 Rust 中,当我们 Box 父结构时,结构中的结构发生了什么。 struct Outer1 { child: Inner1, } struct Inner1 { n: i
我将开始开发一个新网站,并准备处理浏览器用于计算元素宽度和高度的不同方法 (box model stuff)。不知何故,我想到了:如果我只是将 box-sizing 应用于网站中的所有元素会怎么样?
在 java 应用程序中使用 box java sdk 访问 box api 时,我遇到了下面提到的错误。请指出此问题的原因。 Exception in thread "main" com.box.s
计算盒子的宽高时,outline width like outline: 5px dashed red; 如果 box-sizing:border-box,框的宽度/高度是否会考虑轮廓? 最佳答案 我
读完the subtyping chapter of the Nomicon ,我无法理解类型参数的协方差。特别是对于 Box类型,描述为:T is covariant . 但是,如果我写这段代码:
给定这段代码: trait Trait {} struct Child; impl Trait for Child {} struct Father { child: &'a Box, } i
与文件 chooser for dropbox 类似,box.com 有没有? 最佳答案 Box 目前不维护自己的“文件选择器”;然而,有一个很棒的服务叫做 filepicker.io,它充当几乎所有
感谢您对我的问题的回复:Is this a bug of Box API v2 when getting events 这是一个与此相关的新问题。问题是我不能可靠地使用从以前的调用中获得的 next_
我试图让 Box Enterprise API 使用 As-User 工作。我有一个管理员帐户,我用它来尝试检索子帐户中的内容。 我首先使用管理员帐户检索子帐户的用户ID。并将用户 ID 添加为字段“
我想在一个简单的导轨中本地查看我的 Box 帐户中的图像 app .目标是使用这些图像进行幻灯片放映。我寻找必要的参数来传递每个图像相关 url成标签。看来我只能下载图像,或从 Box 应用程序中查看
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (5 个答案) 关闭 4 年前。 给定代码 trait Base {
所以基本上我尝试从https://github.com/Gor-Ren/gym-jsbsim转换这个自定义健身房环境。使用farama基金会的gymnasium api。这是我正在处理的存储库:htt
我是BOX API的新手,因此正在使用API的v2。我正在从我的应用程序本地进行REST调用。 我想上传一个以前可能上传或未上传的文件。我知道父文件夹ID和文件名。我需要覆盖现有文件,或者至少要再
我已被授予访问(协作)文件夹的权限。我需要的是每天访问该文件夹并从中获取文件。现在我生成的开发者 token 将在 1 小时后过期。有什么办法可以得到authorization code没有第一站,它
我正在尝试将 Box 中上传的文件与另一个外部系统同步。我使用哪些 API 来获取“在给定 dd/mm/yy hh:mm:ss 后上传/更新的所有文件的列表”? 干杯! 最佳答案 我认为User Ev
Vagrant 文档和 CLI 使用术语“box”和“base box”。命名意味着基本框是一种特定类型的框,但是,这些术语似乎被用作同义词。 Vagrant“盒子”和 Vagrant“基础盒子”之间
Box 允许您为文件夹创建标签,但如何从 API 中获取该信息?我在 GET /folders/{id} API 中没有看到标签返回.标签也不被视为元数据。 最佳答案 如果你看看 Fields wri
我正在尝试确定应该使用哪个版本的 Box api。我的决定取决于 v1 api 的 EOL 时间线。我不想完成我的应用程序,就在应用程序发布之前(或之后不久),v1 api 停止工作。 我很乐意使用
我是一名优秀的程序员,十分优秀!