These are chat archives for rust-lang/rust

10th
Feb 2018
Alkis Evlogimenos
@alkis
Feb 10 2018 00:33
Is there a way to have a constrain a generic to a specific type instead of a trait? I want a function of slice to be only availble when Self::Item is isize
Kelly Thomas Kline
@kellytk
Feb 10 2018 00:51
Does a convenient API exist for validating a String as containing only valid TOML key identifiers? (A-Za-z0-9_-) I was using is_ascii_alphanumeric but it doesn't allow for underscores, and dashes
Sean Perry
@shaleh
Feb 10 2018 02:35
@kellytk is a regex too heavy handy?
@kellytk self.bytes().all(|b| b.is_ascii_alphanumeric()) that is the implementation of is_ascii_alphanumeric. A similar approach would work for other groups of characters.
Kelly Thomas Kline
@kellytk
Feb 10 2018 06:14
@shaleh Like self.bytes().all(|b| Regex::new(...).is_match(b))?
Sean Perry
@shaleh
Feb 10 2018 06:17
@kellytk I was thinking the regex would just take the string, But you could write a routine like uses is_ascii_alphanumeric as well as checking for underscore and dash. And call that in the all
|b| b.is_asciialphanumeric() || b == b'' || b == b'-'
Kelly Thomas Kline
@kellytk
Feb 10 2018 06:18
Oh, would that even qualify as "a regex"? It's logical comparisons on chars AFAIU
Sean Perry
@shaleh
Feb 10 2018 06:18
no, the regex was my first thought. Then I realized it could done simpler
Kelly Thomas Kline
@kellytk
Feb 10 2018 06:19
Understood, thanks
Sean Perry
@shaleh
Feb 10 2018 06:19
np
sorry, I forgot gitter would eat my code without the backticks
Kelly Thomas Kline
@kellytk
Feb 10 2018 06:46
@shaleh Silly question, do you know what the solution to b == "-" generating the error "expected u8, found reference" is?
red75prime
@red75prime
Feb 10 2018 07:04
b == b'-' if b has u8 type
Kelly Thomas Kline
@kellytk
Feb 10 2018 07:08
Thanks @red75prime
Sean Perry
@shaleh
Feb 10 2018 07:15
That was supposed to be b'_' and b'-'
Kelly Thomas Kline
@kellytk
Feb 10 2018 07:20
Is this notation a kind of type coercion, from '-' char to b'-' u8?
red75prime
@red75prime
Feb 10 2018 09:08
b'X' - byte literal (u8), b"sssdf" - byte slice literal (&'static [u8])
Daniel Bischof
@dbischof90
Feb 10 2018 09:19
Morning fellas
Let's assume I have a BinaryHeap.
How would I delete an element effectively? Normally I would take a Hashtable with references to the elements in the heap, increase the ordered property to a very large value, restore the heap property and pop the top element
Daniel Bischof
@dbischof90
Feb 10 2018 09:25
In Rust we can't do that. So I could transfer it to a vector, search for the index, delete swapremove it and build a heap from the rest again. That should be Ok(n log(n))
*O
Thus much worse. Any alternative ideas?
Aleksey Kladov
@matklad
Feb 10 2018 09:27

@dbischof90 so, the problem is that you can't find an element in the binary heap, right?

The the possible solutions are

1) use a BTreeMap instead (it allows searching)
2) delete elements lazily (i.e, during popmin, check that the poped element is not a tombstone)
3) write a hashmap/heap combo yourself :)

Daniel Bischof
@dbischof90
Feb 10 2018 09:33
@matklad
Oups
Hey!
Well the property to get an extremal element is theain advantage for me to use a binary heap. Does the BTreeMap have that ordered property too?
Aleksey Kladov
@matklad
Feb 10 2018 09:37
@dbischof90 BTreeMap is ordered, so you can get first/last element in O(log(n))
Daniel Bischof
@dbischof90
Feb 10 2018 09:38
3) seems the fastest and cleanest way, but also a little herculean 😄 after all I'm practically a beginner in the language (3 months experience?). I think for now I'll test 2) and make a mental note that I have to find a nicer solutionnfor this and that 😉
Aleksey Kladov
@matklad
Feb 10 2018 09:38
And what is your use-case for binary heap?
Daniel Bischof
@dbischof90
Feb 10 2018 09:39
Oh, great. What's the catch? Any disadvantages in contrast to the binary heap?
Aleksey Kladov
@matklad
Feb 10 2018 09:39
If by any change this is for Dijkstra, I believe the 2) variant works really nice, because you don't even need tombstones.
re BTreeMap vs BinaryHeap, although their asymptotic complexities are almost the same, the heap has much lower constant factor, because it uses a single array for storage.
Daniel Bischof
@dbischof90
Feb 10 2018 09:40
I needed a way to find the maximum of a often changing collection a lot of times. 😋
Ah, I see.
Sarp Güney Başaraner
@sgbasaraner
Feb 10 2018 17:22
Hi folks, I'm trying to get a grasp of Rust by implementing some algorithms. I have question that is rather specific and I couldn't find the exact issue on internet: how do you create multidimensional vectors with a fixed size on both of the vectors? I can't use arrays because the fixed size isn't determined in the runtime.
I'm trying to recreate what would be like "int arr[size1][size2];" in C
red75prime
@red75prime
Feb 10 2018 18:09
let array2: [[i32; 16]; 1024] = [[0; 16]; 1024];, but there's a catch.
let array2: [[i32; 33]; 1024] = [[0; 33]; 1024]; will not work.
kristopher tate
@kristate
Feb 10 2018 21:23

Hi! I have a question about traits and associated functions. I am trying to call Struct<TestEnum>::test() but get the following error:

error: chained comparison operators require parentheses
  --> src/test.rs:32:32
   |
32 |         Struct<TestEnum>::test();
   |                                         ^^^^^
   |
   = help: use `::<...>` instead of `<...>` if you meant to specify type arguments
   = help: or use `(...)` if you meant to specify fn arguments

whereas:

pub trait Struct<T> {
  fn test();
}

pub enum TestEnum {
  Anything,
}

Thank-you for your help!

Aleksey Kladov
@matklad
Feb 10 2018 21:27
@kristate cold you try Struct::<TestEnum>::test()?
kristopher tate
@kristate
Feb 10 2018 21:35
@matklad Thanks for the quick reply, let me check it out and report back
Denis Lisov
@tanriol
Feb 10 2018 21:53
@kristate If there are multiple implementations of Struct<TestEnum> for different types, how do you expect one to be chosen for this call?