These are chat archives for rust-lang/rust

4th
Jan 2019
thojest
@thojest
Jan 04 02:00 UTC
hi guys
Sergey Nikolaev
@qvantor
Jan 04 09:26 UTC
struct Collection {
    objects: Vec<Object>
}

struct StaticCanvas {
    collection: Collection
}

trait Collectable {
    fn get_collection(&self) -> &Collection;
    fn add(&mut self, obj: Object) -> &Collection {
        &self.get_collection().objects.push(obj);   // get cannot borrow as mutable error
        &self.get_collection()
    }
}

impl Collectable for StaticCanvas {
    fn get_collection(&self) -> &Collection {
        &self.collection
    }
}
Who knows, how to fix that? Can not figure out borrowing :(
Michal 'vorner' Vaner
@vorner
Jan 04 09:27 UTC
Add get_collection_mut(&mut self) -> &mut Collection.
Sergey Nikolaev
@qvantor
Jan 04 09:30 UTC
it works, thank.
i miss to pass mutability :)
Sergey Nikolaev
@qvantor
Jan 04 10:26 UTC
trait ObjectTR {
    fn get_object_mut(&mut self) -> &mut Object;
}
trait Collectable {
    fn get_collection_mut(&mut self) -> &mut Collection;
    fn add(&mut self, obj: Box<ObjectTR>) -> bool {
        self.get_collection_mut().objects.push(obj);
        true
    }
    fn remove(&mut self, obj: Box<ObjectTR>) -> bool {
        self.get_collection_mut().objects.remove_item(obj); // the method remove_item exists but the following trait bounds were not //satisfied:std::boxed::Box<(dyn ObjectTR + 'static)> : std::cmp::PartialEq
        true
    }
}
Next code give an error
how can i compare Box of trait objects?
Tim Robinson
@1tgr
Jan 04 10:28 UTC
Box<ObjectTR + PartialEq>
Or:
trait ObjectTR: PartialEq
Sergey Nikolaev
@qvantor
Jan 04 10:31 UTC
It's gave an error the traitObjectTRcannot be made into an object
// primitives
#[derive(PartialEq)]
struct Point {
    pub left: i16,
    pub right: i16,
}

//objects
#[derive(PartialEq)]
struct Object {
    point: Point
}

#[derive(PartialEq)]
struct Rect {
    object: Object
}

impl ObjectTR for Rect {
    fn get_object_mut(&mut self) -> &mut Object {
        &mut self.object
    }
}
Tim Robinson
@1tgr
Jan 04 10:38 UTC
Ok, I see why this can't work... PartialEq tests for equality with a specific type. PartialEq expands to PartialEq<Rhs = Object> or PartialEq<Rhs = Rect>, depending on the type that implements it
Within Box<ObjectTR + PartialEq>, what does PartialEq expand to?
It can't, because the concrete type implementing ObjectTR could be anything, whereas Rect implements PartialEq only for another Rect
Sergey Nikolaev
@qvantor
Jan 04 10:45 UTC
O, i get it. Thanks :)
What the difference between:
impl<'a> PartialEq for ObjectTR + 'a
and impl PartialEq for ObjectTR ?
Tim Robinson
@1tgr
Jan 04 10:52 UTC
There's no difference, mentioning the 'a lifetime is redundant here
Kelly Thomas Kline
@kellytk
Jan 04 10:53 UTC
Is let s: String = String::from(" f o o ").split_whitespace().collect().to_string() the most direct method of stripping any Unicode whitespace from a String?
Kelly Thomas Kline
@kellytk
Jan 04 11:11 UTC
to_string() should be concat()
Denis Lisov
@tanriol
Jan 04 12:39 UTC
@kellytk IMO, one of the following is better
let s: String = String::from(" f o o ").chars().filter(|c| !c.is_whitespace()).collect(); // If you don't want to do that in place
s.retain(|c| !c.is_whitespace()); // If you want to do this in place
Kelly Thomas Kline
@kellytk
Jan 04 13:11 UTC
Why are those better alternatives @tanriol?
Denis Lisov
@tanriol
Jan 04 13:39 UTC
They feel more readable to me. No technical reasons for the first one (and the second may be faster as it is in-place, but that needs benchmarking in your context if you care)
wegry
@wegry
Jan 04 13:43 UTC
Should cargo test hang on
// Ideally this would be a sequence doing something like 0..-8..8..-4..4..-6..6..-2..2
fn spacing_seq(widest: i32, stroke_width: i32) -> Vec<i32> {
    let mut result = vec![0];
    let mut curr = widest - (2 * stroke_width);
    let mut quarter_up = true;

    while curr.abs() != stroke_width {
        result.push(curr);
        if curr > 0 {
            curr = -curr;
        } else if quarter_up {
            curr = (curr / 2) + (-curr);
            quarter_up = false
        } else {
            curr = (-curr) - (curr / 2);
            quarter_up = true
        }
    }

    result
}
#[cfg(test)]
mod tests {
    use super::defaults;

    #[test]
    fn no_overlap() {
        let seq = super::spacing_seq(defaults::COLUMN_GAP as i32, defaults::STROKE_WIDTH);

        for i in 0..=2 {
            assert_eq!(seq[i], vec![20, 18, 16][i])
        }
    }
}
This should terminate right?
When I run with cargo run this func works. But when I try running cargo test one CPU gets pegged and I get the warning test renderer::graph::tests::no_overlap ... test renderer::graph::tests::no_overlap has been running for over 60 seconds
Denis Lisov
@tanriol
Jan 04 13:49 UTC
I don't know the specific argument values you use, but curr = 0 with stroke_width != 0 will, obviously, loop forever from that point.
wegry
@wegry
Jan 04 13:50 UTC
Fair point
Kelly Thomas Kline
@kellytk
Jan 04 14:51 UTC
Thanks @tanriol good suggestion
Genysys
@Genysys
Jan 04 19:29 UTC
Hey, trying to wrap my head around Rust ; are vectors the same thing as lists or arrays in other languages?
Tim Robinson
@1tgr
Jan 04 19:51 UTC
They’re similar to Python lists or Javascript arrays, yes
You can efficiently get or set an element based on its integer index
You can efficiently add and remove items at one end (the back)
You can add and remove elsewhere in the vector, but it’s less efficient
Rust also has arrays, where the length is a compile-time constant