rust

Is there a way to distingush between different `Rc`s of the same value?

試著忘記壹切 提交于 2020-01-24 10:14:45
问题 Here's an example: use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!("rc_a_0 == rc_a_1: {:?}", rc_a_0 == rc_a_1); println!("rc_a_0 == rc_b_0: {:?}", rc_a_0 == rc_b_0); } Both println! s above print true . Is there a way distinguish between the rc_a_* and rc_b_* pointers? 回答1: You can cast &*rc to *const T to get a pointer to the underlying data and

Is there a way to distingush between different `Rc`s of the same value?

て烟熏妆下的殇ゞ 提交于 2020-01-24 10:14:07
问题 Here's an example: use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!("rc_a_0 == rc_a_1: {:?}", rc_a_0 == rc_a_1); println!("rc_a_0 == rc_b_0: {:?}", rc_a_0 == rc_b_0); } Both println! s above print true . Is there a way distinguish between the rc_a_* and rc_b_* pointers? 回答1: You can cast &*rc to *const T to get a pointer to the underlying data and

How can I store an identifier (`proc_macro::Ident`) as a constant to avoid repeating it?

て烟熏妆下的殇ゞ 提交于 2020-01-24 08:26:09
问题 I am writing a procedural macro and I need to emit a very long identifier multiple times (potentially because of hygiene, for example). I use quote! to create TokenStream s, but I don't want to repeat the long identifier over and over again! For example, I want to generate this code: let very_long_ident_is_very_long_indeed = 3; println!("{}", very_long_ident_is_very_long_indeed); println!("twice: {}", very_long_ident_is_very_long_indeed + very_long_ident_is_very_long_indeed); I know that I

How can I store an identifier (`proc_macro::Ident`) as a constant to avoid repeating it?

不想你离开。 提交于 2020-01-24 08:26:06
问题 I am writing a procedural macro and I need to emit a very long identifier multiple times (potentially because of hygiene, for example). I use quote! to create TokenStream s, but I don't want to repeat the long identifier over and over again! For example, I want to generate this code: let very_long_ident_is_very_long_indeed = 3; println!("{}", very_long_ident_is_very_long_indeed); println!("twice: {}", very_long_ident_is_very_long_indeed + very_long_ident_is_very_long_indeed); I know that I

Use of undeclared type or module core::fmt::Display

亡梦爱人 提交于 2020-01-24 05:29:07
问题 I have some code that works fine with exact types, but when I add generics it throws an error. It gives me the following error, my code is below: The trait core::fmt::Display is not implemented for the type T [E0277] fn own<T>(x: T) -> T { x } struct Node<T> { value: T, next: Option<Box<Node<T>>> } impl<T> Node<T> { fn print(&self) { let mut current = self; loop { println!("{}", current.value); match current.next { Some(ref next) => { current = &**next; }, None => break, } } } fn add(&mut

How to write a trait method taking an iterator of strings, avoiding monomorphization (static dispatch)?

纵饮孤独 提交于 2020-01-24 04:00:50
问题 I want to define a trait that has a method operating on sequences of strings. At the same time, I want to avoid having generic methods, a.k.a. static dispatch, in the trait, so that I can use this trait as a trait object. The best solution I was given till now was to do it like below: pub trait Store { fn query_valid_paths(&mut self, paths: &mut dyn Iterator<Item = &str>) -> Vec<String>; } Unfortunately, it's not perfect: It works out of the box only for iterators of &str ; for iterators of

How to get the lower bound and upper bound of an element in a BTreeSet?

∥☆過路亽.° 提交于 2020-01-24 04:00:06
问题 Reading the BTreeSet documentation, I can't seem to figure out how to get the least value greater than, or greatest value less than an element from a BTreeSet in logarithmic time. I see there is a range method that can give the values in an arbitrary (min, max) range, but what if I don't know the range and I just want the previous and/or the next element in logarithmic time? This would be similar to lower_bound and upper_bound in std::set in C++. 回答1: but what if I don't know the range Then

Rust interop with C++ std::string

送分小仙女□ 提交于 2020-01-24 03:30:26
问题 I'm trying to build Octave functions in Rust. Octave's API is in C++, so I've generated bindings using rust-bindgen. I'm currently working through the problems that occur when trying to generate bindings that include std::string. It would be nice if I could leave it opaque and valid pointer to a C++ std::string. Would it be possible to build a utility function on the C++ side any time I needed to pass in a C++ std::string ? I was naive when I first attempted this. It is clearly wrong. A Rust

rust对结构体排序

梦想与她 提交于 2020-01-24 03:29:16
use std : : cmp : : Ordering ; use rand : : Rng ; //导入外部的包... 记得修改toml文件 //保证age是可比较的 pub struct Person < T : std : : cmp : : PartialOrd > { age : T , } //注意泛型T的位置 impl < T > Person < T > where T : std : : cmp : : PartialOrd { //也可impl<T:std::cmp::PartialOrd> Person<T> pub fn new ( a : T ) - > Self { Person { age : a } } } //让Person可比较大小, 操作符重载??? impl < T : std : : cmp : : PartialOrd > PartialOrd for Person < T > { fn partial_cmp ( & self , other : & Self ) - > Option < Ordering > { self . age . partial_cmp ( & other . age ) } } //让Person可比较是否相等, 操作符重载??? impl < T : std : : cmp : : PartialOrd

How do I get a Duration as a number of milliseconds in Rust

不羁的心 提交于 2020-01-24 03:05:39
问题 I have a time::Duration. How can I get the number of milliseconds represented by this duration as an integer? There used to be a num_milliseconds() function, but it is no longer available. 回答1: Here is the solution I came up with, which is to multiply the seconds by a billion, add it to the nanoseconds, then divide by 1e6. let nanos = timeout_duration.subsec_nanos() as u64; let ms = (1000*1000*1000 * timeout_duration.as_secs() + nanos)/(1000 * 1000); 回答2: Use time::Duration from the time