我已经看到了一个与此类似的问题,但没有人告诉我如何实现Ord结构.例如,以下内容:
struct SomeNum {
name: String,
value: u32,
}
impl Ord for SomeNum {
fn cmp(&self, other:&Self) -> Ordering {
let size1 = self.value;
let size2 = other.value;
if size1 > size2 {
Ordering::Less
}
if size1 < size2 {
Ordering::Greater
}
Ordering::Equal
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我错误:
error: the trait `core::cmp::Eq` is not implemented for the type `SomeNum` [E0277]
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我尝试将实现更改为:
impl Ord for SomeNum where SomeNum: PartialOrd + PartialEq + Eq {...}
Run Code Online (Sandbox Code Playgroud)
并添加适当的partial_cmp和eq函数,但它给我的错误,这两个方法都不是成员Ord.
我正在尝试使用a Condvar来限制在任何给定时间处于活动状态的线程数.我很难找到如何使用的好例子Condvar.到目前为止,我有:
use std::sync::{Arc, Condvar, Mutex};
use std::thread;
fn main() {
let thread_count_arc = Arc::new((Mutex::new(0), Condvar::new()));
let mut i = 0;
while i < 100 {
let thread_count = thread_count_arc.clone();
thread::spawn(move || {
let &(ref num, ref cvar) = &*thread_count;
{
let mut start = num.lock().unwrap();
if *start >= 20 {
cvar.wait(start);
}
*start += 1;
}
println!("hello");
cvar.notify_one();
});
i += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
给出的编译器错误是:
error[E0382]: use of moved value: `start`
--> src/main.rs:16:18
|
14 | …Run Code Online (Sandbox Code Playgroud)