小编Dum*_*les的帖子

多行字符串文字的语法是什么?

我很难弄清楚Rust中的字符串语法是如何工作的.具体来说,我试图找出如何制作多行字符串.

string rust

95
推荐指数
5
解决办法
3万
查看次数

如何为结构实现Ord?

我已经看到了一个与此类似的问题,但没有人告诉我如何实现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_cmpeq函数,但它给我的错误,这两个方法都不是成员Ord.

rust ord

17
推荐指数
1
解决办法
4317
查看次数

如何使用Condvar限制多线程?

我正在尝试使用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)

multithreading rust

5
推荐指数
1
解决办法
1313
查看次数

标签 统计

rust ×3

multithreading ×1

ord ×1

string ×1