拥有VecDeque deq和item,我如何找出是否deq包含item?我可以用迭代器做到这一点:
deq.iter().find(|e| e == item).is_none()
Run Code Online (Sandbox Code Playgroud)
但这是很多代码。我只想说deq.contains(item),但是VecDeque没有这样的方法(编辑:不再是真的,请参见我的回答)。还有其他选择吗?
这是我的代码:
struct Node<T> {
data: T,
next: Option<Box<Node<T>>>,
}
impl<T> Node<T> {
fn new(data : T) -> Node<T> {
Node { data: data, next: None }
}
fn new_with_next(data: T, next: Option<Box<Node<T>>>) -> Node<T> {
Node { data: data, next: next }
}
}
struct LinkedList<T> {
head: Box<Node<T>>,
size: u8,
}
impl<T> LinkedList<T> {
fn new(data: T) -> LinkedList<T> {
let new_node = Node::new(data);
let head = Box::new(new_node);
LinkedList { head: head, size: 1 }
}
fn insert(&mut self, …Run Code Online (Sandbox Code Playgroud) 为了在阻止移动时匹配结构,我想使用引用来进行匹配.所以下面的代码将完成这项工作:
struct Foo(i32);
fn main() {
let x = Foo(1);
match &x {
ref a => println!("hello"),
}
println!("{}", x.0);
}
Run Code Online (Sandbox Code Playgroud)
但我很惊讶地发现以下代码也有效,它实际上将struct(x)与reference(ref a)匹配.但是,这不应该是类型不匹配吗?
struct Foo(i32);
fn main() {
let x = Foo(1);
match x {
ref a => println!("hello")
}
println!("{}", x.0);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用多个请求多个网址std::thread.这是我的代码到目前为止的样子:
fn fetch(urls: Vec<&str>) {
let (tx, rx) = mpsc::channel();
for url in urls {
let tx = tx.clone();
thread::spawn(|| {
let ssl = NativeTlsClient::new().unwrap();
let connector = HttpsConnector::new(ssl);
let client = Client::with_connector(connector);
let mut res = client.get(url).send().unwrap();
let mut result = String::new();
res.read_to_string(&mut result);
tx.send(result).unwrap();
});
}
//let mut result: Vec<String> = vec![];
for _ in urls {
println!("{}", rx.recv().unwrap());
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误说:
error[E0277]: the trait bound `std::sync::mpsc::Sender<std::string::String>: std::marker::Sync` is not satisfied
--> src/lib.rs:18:9
|
18 | …Run Code Online (Sandbox Code Playgroud) 使用相互引用的结构在单个文件中很常见,但是当我将结构分成两个文件时,我收到错误.
mod_1.rs
mod mod_2;
use mod_2::Haha;
pub struct Hehe {
obj: Haha,
}
fn main() {
Hehe(Haha);
}
Run Code Online (Sandbox Code Playgroud)
mod_2.rs
mod mod_1;
use mod_1::Hehe;
pub struct Haha {
obj: Hehe,
}
fn main() {
Haha(Hehe);
}
Run Code Online (Sandbox Code Playgroud)
这将产生错误.编译时mod_1.rs:
error: cannot declare a new module at this location
--> mod_2.rs:1:5
|
1 | mod mod_1;
| ^^^^^
|
note: maybe move this module `mod_2` to its own directory via `mod_2/mod.rs`
--> mod_2.rs:1:5
|
1 | mod mod_1;
| ^^^^^
note: ... …Run Code Online (Sandbox Code Playgroud) 阅读Rust教程我发现了以下代码片段:
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e)
};
Run Code Online (Sandbox Code Playgroud)
这似乎f可以具有type Result<_, _>(if f == Err(e))的值或类型的值_(即,无论何种类型file).
那么这是否意味着Rust中的匹配表达式是不确定的类型?
我的部分代码如下所示:
print_usage_instructions();
print!("Command: ");
let stdin = io::stdin();
let mut line = String::new();
stdin.lock().read_line(&mut line).expect("Couldn't process the command.");
println!("{}", line);
Run Code Online (Sandbox Code Playgroud)
我期望的行为是这样的:
Usage instructions and stuff
Command: [my command]
[my command]
Run Code Online (Sandbox Code Playgroud)
但是,会发生什么:
Usage instructions and stuff
[my command]
Command: [my command]
Run Code Online (Sandbox Code Playgroud)
任何想法为什么会发生?AFAIK,编译器没有理由在这里更改执行顺序,这部分代码不是异步也不是多线程.
在 Python 中,我了解到可以通过动态索引检索元组值:
data = (1,2,3,4)
data[0]
for a in range(len(data)):
print(data[a])
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
如何在 Rust 中做到这一点?
我试过这样的事情:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
但它不起作用,它只会给我一些错误。
我原以为dbg!(x)和println!("{:?}", x)既能打印相同的字符串,因为{:?}用于调试输出,对不对?
如果我没记错的话,为什么我们会看到这个结果,一个显示在单行中,而另一个显示在多行中?
fn main() {
let x: Vec<u32> = (0..10).collect();
println!("{:?}", x[0..1].to_vec());
dbg!(x[0..1].to_vec());
}
Run Code Online (Sandbox Code Playgroud)
[src/main.rs:4] x[0..1].to_vec() = [
0,
]
[0]
Run Code Online (Sandbox Code Playgroud)
I'm trying to implement common trait for a bunch of types created from binary data (read from a disk). Majority of trait methods could use default implementations and only conversions etc. would be needed to be implemented separately. I would like to use TryFrom<&[u8]> trait for conversions from binary data to my types but I don't know how to express (in the context of trait) that lifetime of &[u8] and lifetimes of values of my types created from it are …