我有返回的行Result
。结果并不重要,我不想要unwrap()
或任何日志记录。(预期频繁失败)
我怎样才能让这些行的未使用警告静音?
#[allow(unused_must_use)]
应用于 fn 级别时似乎有效,但这不起作用:
#[allow(unused_must_use)]
std::fs::remove_file(&path1);
#[allow(unused_must_use)]
std::fs::remove_file(&path2);
Run Code Online (Sandbox Code Playgroud)
编辑:
whilelet _ = ..
作为一种解决方法,#[allow(unused..)]
专门针对这种情况而存在,并且编译器也建议使用它。阅读let _ = ..
增加了另一层思维。(分配然后放弃)所以我更喜欢#[allow(..)]
虽然它更冗长。(如果我let _ = ..
经常看到并且习惯了,我可能会改变我的偏好)
所以我四处搜索,发现了一些将宏应用于语句级别的代码。这个问题询问为什么#[allow(unused_must_use)]
一行不起作用 - 语法错误?漏洞?只是尚未实现unused_must_use
?
编辑: 根据这个:
如何在 Rust 中消除单个语句的警告? 我的代码应该可以工作。
阅读这些:
https://github.com/rust-lang/rust/issues/36675
https://github.com/rust-lang/rust/issues/15701
我在范围级别尝试了允许并且它起作用了。
#[allow(unused_must_use)] {
std::fs::remove_file(&path1);
std::fs::remove_file(&path2);
}
Run Code Online (Sandbox Code Playgroud) 我想在 trait 中编写异步函数,但由于async fn
尚不支持 in trait,我试图找到等效的方法接口。这是我每晚在 Rust 中尝试过的(2019-01-01):
#![feature(await_macro, async_await, futures_api)]
#[macro_use]
extern crate tokio;
use tokio::prelude::*;
trait T {
async fn f();
}
fn main() {
}
Run Code Online (Sandbox Code Playgroud)
error[E0706]: trait fns cannot be declared `async`
--> src/main.rs:7:5
|
7 | async fn f();
| ^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
我在某处读到async
只是impl Future
.
trait T {
fn f() -> impl futures::Future<Item = (), Error = ()>;
}
Run Code Online (Sandbox Code Playgroud)
error[E0562]: `impl Trait` not allowed outside of function and inherent method return …
Run Code Online (Sandbox Code Playgroud) 考虑一个具有道具“名称”和状态“经过”的组件。
new Component(name) => "Hi {name}. It's been {elapse} seconds"
Run Code Online (Sandbox Code Playgroud)
{elapse}
道具{name}
更改时应将其重置为0 。
如果道具在10秒内从“爱丽丝”变为“鲍勃”,则消息应从
嗨,爱丽丝。已经十秒钟了
至
嗨,鲍勃。到了0秒
getDerivedStateFromProps
不能使用,因为{elapse}
它不是的纯函数{name}
,我不能返回0,因为它可能在重新渲染时被调用。
componentDidUpdate
最终将更新{elapse}
为0,但在此之前,会向用户显示无效状态“嗨,鲍勃。已经为0秒”。
可以getDerivedStateFromProps
或componentDidUpdate
实现这个场景?
在许多情况下,状态不是道具的纯粹功能。是getDerivedStateFromProps
仅适用于无状态的功能部件?反应是否鼓励使用无状态组件?
如何getDerivedStateFromProps
替换componentWillReceiveProps
有状态组件?
我有一个BTreeMap<f64,_>
并想x
在其中留下号码low < x < high
。但split_off(k)
无法控制k
包容性或排他性。
这是 eps 的临时解决方案:
// delete all numbers >= high
let _ = map.split_off(high);
// want to delete all numbers <= low
map = map.split_off(low+1e-8);
Run Code Online (Sandbox Code Playgroud)
正确的做法是把元素一一删除,我认为是可以采取的O(n lg n)
。有没有办法使用map.range((Excluded(&low), Excluded(&high)))
和删除 中的范围O(lg n)
?看起来我需要map.erase(range)
或map.split_off_after(k)
。
我把它放在我的Cargo.toml中
[build]
target-dir = "../my-target"
Run Code Online (Sandbox Code Playgroud)
但是Cargo不承认这个钥匙.
cargo run --release --bin my_project
warning: unused manifest key: build
error: failed to open: /.../project-root/target/releases/.cargo-lock
Caused by:
Permission denied (os error 13)
Run Code Online (Sandbox Code Playgroud)
带有环境变量的自定义目标目录有效:
CARGO_TARGET_DIR=../my-target cargo run --bin my_project
Run Code Online (Sandbox Code Playgroud)
但是如何在Cargo.toml中指定"../my-target"
多流(yamux,多流选择,..)和多路复用(mplex)之间有什么区别?我想将一个TCP连接用于RPC,HTTP等(一个客户端位于防火墙后面),如下所示:
conn = tcp.connect("server.com:1111")
conn1, conn2 = conn.split()
stream1 = RPC(conn1)
stream2 = WebSocket(conn2)
..
// received packets tagged for conn1 is forwarded to stream1
// received packets tagged for conn2 is forwarded to stream2
// writing to stream1 tags the packets for conn1
// writing to stream2 tags the packets for conn2
Run Code Online (Sandbox Code Playgroud)
哪一个适合这种情况?