我有一些想要使用 Serde 的结构。像这个:
use serde::{Serialize, Deserialize};
#[derive(Serialize)]
struct GetLinkResponse {
error: String,
link: String,
}
Run Code Online (Sandbox Code Playgroud)
但编译器说:
Serialize导入未使用Serialize找不到use serde::{Serialize, Deserialize};
#[derive(Serialize)]
struct GetLinkResponse {
error: String,
link: String,
}
Run Code Online (Sandbox Code Playgroud)
我想我不明白use是如何工作的。有人可以解释一下我的错误是什么吗?
编者注:在Rust 1.0之前问过这个问题,问题中的一些断言在Rust 1.0中不一定正确.一些答案已更新,以解决这两个版本.
我想创建一个向量,但我只知道我希望向量在运行时的大小.这就是我现在正在做的事情(即创建一个空的,可变的向量,并向其添加向量):
fn add_pairs(pairs: ~[int]) -> ~[int] {
let mut result : ~[int] = ~[];
let mut i = 0;
while i < pairs.len() {
result += ~[pairs[i] + pairs[i + 1]];
i += 2;
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的方式(即创建一个向量并将所有内容放入其中,而不是一起添加大量向量):
fn add_pairs(pairs: ~[int]) -> ~[int] {
let number_of_pairs = pairs.len() / 2;
let result : ~[int, ..number_of_pairs];
let mut i = 0;
while i < pairs.len() {
result[i] = pairs[2 * i] + pairs[2 * i + …Run Code Online (Sandbox Code Playgroud) 2017年的新标准增加了std::filesystem.使用它,我如何计算目录中的文件(包括子目录)的数量?
我知道我们可以这样做:
std::size_t number_of_files_in_directory(std::filesystem::path path)
{
std::size_t number_of_files = 0u;
for (auto const & file : std::filesystem::directory_iterator(path))
{
++number_of_files;
}
return number_of_files;
}
Run Code Online (Sandbox Code Playgroud)
但这似乎有点矫枉过正.是否存在更简单,更快捷的方法?
看到这段代码:
fn main() {
let something_const = 42;
fn multiply(nbr: i32) -> i32 {
nbr * something_const
}
println!("{}", multiply(1));
}
Run Code Online (Sandbox Code Playgroud)
rustc 输出那个
error[E0434]: can't capture dynamic environment in a fn item; use the || { ... } closure form instead
--> main.rs:19:15
|
19 | nbr * something_const
| ^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
但something_const它不是动态的,因为它在编译时是已知的.
它在Rust的C++ constexpr机制中是等价的吗?
我的Rust程序旨在逐行读取非常大(最多几GB)的简单文本文件.问题是,此文件太大而无法立即读取,或将所有行传输到a Vec<String>.
在Rust中处理这个问题的惯用方法是什么?
以下Rust代码无法编译:
enum Foo {
Bar,
}
impl Foo {
fn f() -> Self {
Self::Bar
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息让我困惑:
error[E0599]: no associated item named `Bar` found for type `Foo` in the current scope
--> src/main.rs:7:9
|
7 | Self::Bar
| ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)
这个问题可以通过使用Foo而不是来解决Self,但这让我觉得奇怪,因为Self它应该引用正在实现的类型(忽略特征),在这种情况下是Foo.
enum Foo {
Bar,
}
impl Foo {
fn f() -> Self {
Foo::Bar
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不能Self在这种情况下使用?哪里可以Self使用*?还有什么我可以用来避免在方法体中重复类型名称吗?
*我忽略了traits中的用法,其中Self指的是实现特征的任何类型.
Mutex 一次只能有一个读者或作者,RwLock 一次可以有一个作家或多个读者.当你这样说的时候,RwLock似乎总是更好(更少限制)Mutex,为什么我会使用它呢?
一个pull请求已经与锈编译一个新的测试完成.它验证一个奇怪的行可以编译:
fn main() {
let val = !((|(..):(_,_),__@_|__)((&*"\\",'@')/**/,{})=={&[..=..][..];})//
;
assert!(!val);
}
Run Code Online (Sandbox Code Playgroud)
这条线到底做了什么?
我试图在后台运行一个线程,然后更改 AtomicBool 来要求线程停止。为了确保线程正确停止,我想调用joinJoinHandle 的方法。
thread::sleep_ms如果我在设置 AtomicBool 后等待 () 一段时间,而不是调用 join ,线程就会正确关闭。但为了确保这一点,我想使用 join。或者有没有更好的方法来确保线程正确关闭?
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::thread::JoinHandle;
struct MyInner { s: String }
struct My {
inner: Arc<Mutex<MyInner>>,
close_thread: Arc<AtomicBool>,
my_thread: JoinHandle<()>
}
impl MyInner {
fn new(s: String) -> MyInner {
MyInner {
s: s
}
}
}
impl My {
fn new(s: String) -> My {
My {
inner: Arc::new(Mutex::new(MyInner::new(s))),
close_thread: Arc::new(AtomicBool::new(false)),
my_thread: thread::spawn(move || {})
}
}
fn …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中是如何工作的.