These are chat archives for rust-lang/rust

11th
Sep 2018
Wang Shidong
@wsdjeg
Sep 11 2018 05:37
any one use vim as rust IDE? I am author of spacevim, and we just add lang#rust layer. we hope rust developer can help to try it. and feedback is welcome . https://spacevim.org/layers/lang/rust/ in this layer we use deoplete + racer by default . but as I know rust has provide language server which implement Microsoft's lsp feature.
Kelly Thomas Kline
@kellytk
Sep 11 2018 07:08
Is either String or str preferred for comparing strings with if foo == bar?
Vitaly
@Virtuos86
Sep 11 2018 08:50
of course string slices, &str
Zakarum
@omni-viral
Sep 11 2018 09:27
It is irrelevant actually
trsh
@trsh
Sep 11 2018 09:56
Can I conver json::Value to HashMap in on command?
Juan Aguilar
@botika
Sep 11 2018 10:15
There is no equivalence
Exactly, a Json with two properties of different type.
Kelly Thomas Kline
@kellytk
Sep 11 2018 10:24
@Virtuos86 Why do you think?
Why would a String "ping" not match (==) String::from("ping")? I'm sending a message over WebSocket to actix web and although I trim() and println, and see "ping" in the output, it doesn't match
Zakarum
@omni-viral
Sep 11 2018 10:44
fn main() { println!("{}", "ping" != "рing");
Check it on play.rust-lang.org
Kelly Thomas Kline
@kellytk
Sep 11 2018 10:48
Why is it giving that result @omni-viral?
Magic
Kelly Thomas Kline
@kellytk
Sep 11 2018 10:50
Ok but that doesn't help me comprehend why comparing two of the same strings fails
Juan Aguilar
@botika
Sep 11 2018 10:50
@omni-viral woooh!! one punch
Zakarum
@omni-viral
Sep 11 2018 10:51
It is a joke. Strings are actually different.
Kelly Thomas Kline
@kellytk
Sep 11 2018 10:53
Do you know why I'm encountering the problem?
Zakarum
@omni-viral
Sep 11 2018 10:54
I have no idea. Except maybe you comparing them wrong way
Kelly Thomas Kline
@kellytk
Sep 11 2018 10:54
How should two Strings be compared?
Zakarum
@omni-viral
Sep 11 2018 11:16
Try to print both compared string. Try to print them as bytes
SaturnTeam
@SaturnTeam
Sep 11 2018 11:55

hi! I tried to play with traits, structs and implementation, and still can't understand, how could I have one struct and two different implementation for it?

#[derive(Debug)]
struct QuickFindUF {
    count: usize,  // count of components (independent graphs)
    id: Vec<i32>, // connected items. index as source and value as destination
}

impl QuickFindUF {
}

struct QuickUnionUF {
    count: usize, // count of components (independent graphs)
    id: Vec<i32>, // connected items. index as source and value as destination
}

impl QuickUnionUF {
}

I tried everything what I found, but the only working solution is duplicating the structure :(

SaturnTeam
@SaturnTeam
Sep 11 2018 12:00
some functions have the same implementations
Lyle Mantooth
@IslandUsurper
Sep 11 2018 12:43
@SaturnTeam, it sounds like you want two traits. But the better solution is maybe just having one impl with different functions in it.
SaturnTeam
@SaturnTeam
Sep 11 2018 12:44
@IslandUsurper To be honest, they can't have different functions, because the values in id have different meaning
Lyle Mantooth
@IslandUsurper
Sep 11 2018 12:45
Now I'm lost. What are you really trying to do?
Because it sounded like QuickFindUF and QuickUnionUF were identical structs, holding the same data.
SaturnTeam
@SaturnTeam
Sep 11 2018 12:51
@IslandUsurper I'm really trying to use the same struct definition. I'll show some code. Pay attention only in implementation of union. id[some_key]=q_id in first implementation may be applied several times, but in the second only once.

#[derive(Debug)]
struct QuickFindUF {
    count: usize, // count of components (independent graphs)
    id: Vec<i32>, // connected items. index as source and value as destination
}

impl QuickFindUF {
    fn connected(&self, p: i32, q: i32) -> bool {
        self.find(p) == self.find(q)
    }
    fn find(&self, p: i32) -> i32 {
        self.id[p as usize]
    }
    fn union(&mut self, p: i32, q: i32) {
        let p_id: i32 = self.find(p);
        let q_id = self.find(q);
        if p_id == q_id {
            return;
        }
        self.id.iter_mut()
            .filter(|id| **id == p_id)
            .for_each(|id| *id = q_id);
        self.count -= 1;
    }
}

#[derive(Debug)]
struct QuickUnionUF {
    count: usize, // count of components (independent graphs)
    id: Vec<i32>, // connected items. index as source and value as destination
}

impl QuickUnionUF {
    fn connected(&self, p: i32, q: i32) -> bool {
        self.find(p) == self.find(q)
    }
    fn find(&self, pp: i32) -> i32 {
        let mut p: usize = pp as usize;
        while p as i32 != self.id[p] {
            p = self.id[p] as usize;
        }
        p as i32
    }
    fn union(&mut self, p: i32, q: i32) {
        let p_id: i32 = self.find(p);
        let q_id = self.find(q);
        if p_id == q_id {
            return;
        }
        self.id[p_id as usize] = q_id;
        self.count -= 1;
    }
}
The first one have fast find function, and the second have fast union function. The base structure is the same. And I want to use it in both implementation. And I tied fighting with rust compiler. He always wins and makes me duplicate the code. :(
Lyle Mantooth
@IslandUsurper
Sep 11 2018 12:56
OK, so the reason you would use one find or the other or one union or the other is because of the specific data contained in the struct?
How it's shaped and "connected"?
SaturnTeam
@SaturnTeam
Sep 11 2018 12:59
@IslandUsurper Yes, but mostly, I'm interested in how to solve this problem.
Denis Lisov
@tanriol
Sep 11 2018 13:00
@SaturnTeam Let's ask the other way around: why would you prefer to use the same structure with different interpretation?
SaturnTeam
@SaturnTeam
Sep 11 2018 13:02
@tanriol 1) Less code 2) also functions connected and fn count(&self) -> usize { self.count } are the same, and I'd like to have only one implementation of them too
Lyle Mantooth
@IslandUsurper
Sep 11 2018 13:05
Leaving aside whether it's a good idea this time, here's what I'd do: https://play.rust-lang.org/?gist=37f58c87682851723c4fa0d2abda5054&version=stable&mode=debug&edition=2015
@SaturnTeam, however, you aren't actually required to use the same structure this way. If there's a better design for QuickUnionUF as a struct, you can do it. You'd only have to change its impl of Connected.
SaturnTeam
@SaturnTeam
Sep 11 2018 13:12

It almost works with the trait https://gist.github.com/d3ac10944c933bc070f7e5c1bb33455e
But then I try implement count in trait

    fn count(&self) -> usize {
        self.count
    }

I got:

error[E0615]: attempted to take value of method `count` on type `&Self`
  --> src/alg/uf.rs:17:14
   |
17 |         self.count
   |              ^^^^^
   |
   = help: maybe a `()` to call it is missing?

error: aborting due to previous error

For more information about this error, try `rustc --explain E0615`.
error: Could not compile `a`.
Lyle Mantooth
@IslandUsurper
Sep 11 2018 13:13
Because Connected can be implemented for any type, its default methods can't assume there is a count field.
SaturnTeam
@SaturnTeam
Sep 11 2018 13:14
And there is no way to specify the type?
Lyle Mantooth
@IslandUsurper
Sep 11 2018 13:14
So count() has to go in the impls.
Or, you can just put impl QuickUnionUF { pub fn count(&self) -> usize { self.count }
It doesn't have to be part of Connected either.
Depends on what the underlying meaning of all this is.
SaturnTeam
@SaturnTeam
Sep 11 2018 13:17
As anyone, I try to apply my knowledge and typical thinking in OOP to rust. And it's not working
Justin Kilpatrick
@jkilpatr
Sep 11 2018 13:30
so is there any way to use join all where some instances can fail?
for futures
tandrysyawaludin
@tandrysyawaludin
Sep 11 2018 15:26
anyone knows why i got this error?
error[E0061]: this function takes 1 parameter but 2 parameters were supplied
  --> src/partner_module/partner.rs:95:15
   |
95 |       .filter(sql::<Bool>("category_of_trash_id @> ARRAY[{}]::text[]", &category))
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter
actually i just give one parameter,it is &category
Merlijn Boogerd
@mboogerd
Sep 11 2018 16:05
I am experiencing an error across libraries, that the soft-deprecated method description in std::error::Error impls is missing. Is there something I am doing wrong?
Kelly Thomas Kline
@kellytk
Sep 11 2018 21:59
@omni-viral The string that was sent over the WS has a 34 at the beginning and end which is ". What must be happening is that I encode the text as JSON on the sending side of the WS but I haven't decoded it on the receiving end and in JSON format the string will have quotes. Thanks for your help