These are chat archives for rust-lang/rust

Jul 2018
Jul 21 2018 01:18
// Definition for binary tree:
// struct Tree<T> {
//     value: T,
//     left: TreeNode<T>,
//     right: TreeNode<T>
// }
// impl<T> Tree<T> {
//     fn new(v : T) -> Tree<T> {
//         Tree { value: v, left: None, right: None }
//     }
// }
// type TreeNode<T> = Option<Box<Tree<T>>>;
fn hasPathWithGivenSum(t: TreeNode<i32>, s: i32) -> bool {
    // Depth-first search
    let mut sum = 0;
    let mut stack: Vec<Box<Tree<i32>>> = vec![t.unwrap()];
    while !stack.is_empty() {
        let current: Box<Tree<i32>> = stack.pop().unwrap();
        match *current {
            Tree { left: Some(lnode), right: Some(rnode), value: v} => {
                // complains about the two Some(xnode)
                // error[E0382]: use of moved value: `(current.right as std::prelude::v1::Some)
Pattern match on structs does not work with boxes. What to do?
Sai Zeng
Jul 21 2018 10:22

I'm using #![feature(rust_2018_preview)] in my lib, and write this code:

pub enum Error {

I got this error:

[rustc] cannot find derive macro `Display` in this scope
How to import std macro into lib scope ?
Denis Lisov
Jul 21 2018 10:34
@zengsai Display is intended to be a representation for humans and thus cannot be derived. Did you mean to use Debug?
@tsoernts Do you really want this method to consume the tree?
Sai Zeng
Jul 21 2018 11:02
Oh, you are right!
Jul 21 2018 11:03
Can we merge Json serde objects?
Jul 21 2018 11:18
Looks like there is nothing built in, but found a snippet
Btw, how Do i feed closure function to a normal Function?
For inside processing
Denis Lisov
Jul 21 2018 11:22
@trsh To pass a closure as an argument, you need a bound of Fn/FnMut/FnOnce, depending on how you want to call it.
Robin Miller
Jul 21 2018 16:50
@trsh if you want to define a function that takes a function as an argument, you can use fn func<F, R>(func: F) where F: FnOnce() -> R {...}.
Adler Oliveira
Jul 21 2018 17:37
Hello all, quick question: How do I make a thread with a loop inside do exactly 60 cycles per second?
Jul 21 2018 18:12
How exact are we talking? And will you always have less than 16 ms of work per cycle, or do you need to start the next cycle before the previous one finishes sometimes?
Jul 21 2018 18:22
If you don't have much work to do, I'm not sure whether there is something built in, but it's not very hard to roll your own by using to measure the current time and then sleep ( until the next cycle starts. The accuracy will probably only be good to within a couple ms, though, and how accurate you are will depend on platform and the load of the machine
Denis Lisov
Jul 21 2018 18:45
@adleroliveira Is this a display loop? Pretty sure you want to sync to vblank, no?
Jul 21 2018 18:48
What display runs at 60 Hz any more?
Denis Lisov
Jul 21 2018 18:49
I'm pretty sure my laptop's one does :-)
Jul 21 2018 19:03
Okay, I guess it looks pretty common for laptops, but it's not something you can count on on an arbitrary machine. That it is for a display is a good guess, though, and you are probably right that syncing to vblank is a better idea since 60 Hz updates with, say, 100Hz refresh looks lousy.
Otto Chrons
Jul 21 2018 20:55
not to mention GSync etc with variable fresh rates ;)