These are chat archives for rust-lang/rust

21st
Sep 2017
lemonxah
@lemonxah
Sep 21 2017 10:39
any macro guys here? https://play.rust-lang.org/?gist=04c44761a56b8bc3be493d51ffc7c2ec&version=nightly i would love some input how i could do this better
its because i have to write 40-50 microservices and i dont want to write the same code over and over again
Tyler Neely
@spacejam
Sep 21 2017 12:44
@lemonxah could you replace $data and $get with a closure?
Tyler Neely
@spacejam
Sep 21 2017 12:54
a thing I am trying to get into the better habit of is when I am that deeply nested, try to pick the pieces apart
for me it's fun to chain combinators like crazy but then I come back the next day and have trouble understanding my own decisions
one trick for detangling gnarly macros is to reduce their arguments and use other macros / recursion with tokens (although this can hit recursion limits quickly)
Tyler Neely
@spacejam
Sep 21 2017 13:00
I'm looking forward to https://doc.rust-lang.org/std/macro.concat_idents.html stabilizing to chop down a few of my bigger ones
Fra ns
@snarf95_twitter
Sep 21 2017 13:23
any idea on how I could improve this code in terms of readability and performance? http://exercism.io/submissions/2276dccf533e4484ae56db3ca0b5ab1e
Denis Lisov
@tanriol
Sep 21 2017 13:30
Using standard library only or using other libraries too if needed?
Fra ns
@snarf95_twitter
Sep 21 2017 13:59
Other libraries too I guess
Denis Lisov
@tanriol
Sep 21 2017 14:00
Maybe look at using rayon?
Fra ns
@snarf95_twitter
Sep 21 2017 14:00
hmm yea but would that help with performance?
Denis Lisov
@tanriol
Sep 21 2017 14:00
Probably not :-)
Fra ns
@snarf95_twitter
Sep 21 2017 14:00
I think usage of HashMap is the real performance killer in this case
Steve Klabnik
@steveklabnik
Sep 21 2017 14:00
it may
Denis Lisov
@tanriol
Sep 21 2017 14:00
The performance seems already close to optimal.
Steve Klabnik
@steveklabnik
Sep 21 2017 14:01
it would simplify the code a lot
@snarf95_twitter you may want to use a different hash function
Fra ns
@snarf95_twitter
Sep 21 2017 14:01
@steveklabnik thanks, gonna have a look
TatriX
@TatriX
Sep 21 2017 14:58
Crate piston_window reexports module pub use graphics::*;
How can I access it's reexported functions?
Steve Klabnik
@steveklabnik
Sep 21 2017 14:59
they'd be under piston_window::some_reexported_function
TatriX
@TatriX
Sep 21 2017 15:00
Parent crate has pub use graphics::*;
Which has pub use image::Image;
And it has a required function new
I'm trying to understand how can I call Image::new()
Oh, nevermind. I can call it. It's just racer which can't do an autocompletion.
Steve Klabnik
@steveklabnik
Sep 21 2017 15:15
:smile: :+1:
TatriX
@TatriX
Sep 21 2017 15:24
I have a Vec<Vec<u32>>, and I want to iterate over it
            for row in layer.tiles.iter() {
                for tile in row {
Here in tile I'm getting &u32. How can I get u32? Sure I can do *tile but it seems wrong.
Steve Klabnik
@steveklabnik
Sep 21 2017 15:25
you want into_iter
not iter
iter iterates by reference, so always gives &T
that asid
said
you may instead want .iter().cloned()
depending, that is, moving out will destroy tiles
TatriX
@TatriX
Sep 21 2017 15:26
cannot move out of borrowed content
Steve Klabnik
@steveklabnik
Sep 21 2017 15:26
so, iter().cloned() ends up taking the &u32 and basically does the * for you
yeah, so go with .iter().cloned()
Jonas Platte
@jplatte
Sep 21 2017 15:27
u32 is Copy... Why not for &tile in row?
TatriX
@TatriX
Sep 21 2017 15:27
Cool, thanks!
Steve Klabnik
@steveklabnik
Sep 21 2017 15:27
yeah that'd work too, i guess
Jonas Platte
@jplatte
Sep 21 2017 15:28
Also, for row in layer.tiles.iter() can be shortened to for row in &layer.tiles :)
TatriX
@TatriX
Sep 21 2017 15:29
I was giving me an error, but it's not now.
It's even more cooler , haha
So if I do for row in &layer.tiles {
The type of row is &Vec<T>`?
Steve Klabnik
@steveklabnik
Sep 21 2017 15:29
well
the type of &layer.tiles is &Vec<T>
&Vec<T> implements IntoIterator where Item=&T
and for calls IntoIterator
so row is &T
TatriX
@TatriX
Sep 21 2017 15:30
I need an emacs extenstion which will do let () = row; for me, haha
Steve Klabnik
@steveklabnik
Sep 21 2017 15:30
:) i think emacs works with racer/the rls, right?
steveklabnik @steveklabnik is more of a vim/vs: code person
TatriX
@TatriX
Sep 21 2017 15:31
It is, but it cant show me the time under the cursor in most of the situations
TatriX
@TatriX
Sep 21 2017 15:45
So if row is &Vec<T>, why tile in for &tile in row { is T?
Steve Klabnik
@steveklabnik
Sep 21 2017 15:45
so
for THIS_PART in
THIS_PART is a pattern
&foo is a pattern that says "the thing i match against is of type &T, bind foo to that T"
as such, this only works for Copy types
TatriX
@TatriX
Sep 21 2017 15:46
Ohh, it makes sense now
Thanks again!
Steve Klabnik
@steveklabnik
Sep 21 2017 15:46
:+1:
TatriX
@TatriX
Sep 21 2017 15:47
I was thinking of & a "take reference"
Steve Klabnik
@steveklabnik
Sep 21 2017 15:47
yeah, patterns are backward
TatriX
@TatriX
Sep 21 2017 15:47
but in let x = &xs it's "take reference" is it?
while let &x = xs it's pattern matching
Jonas Platte
@jplatte
Sep 21 2017 15:48
Yes. The left side of let is a pattern, the right side is an expression
Steve Klabnik
@steveklabnik
Sep 21 2017 15:48
yup!
TatriX
@TatriX
Sep 21 2017 15:48
Cool, thanks ;)
TatriX
@TatriX
Sep 21 2017 18:05
I'm looking for a person experienced in piston. Could you please check if this looks fine: https://github.com/TatriX/rs-tiled-piston-example
Michal 'vorner' Vaner
@vorner
Sep 21 2017 18:35
Oh, great. Rustc crashes on my code with stack overflow.
diegor8
@diegor8
Sep 21 2017 18:35
@vorner please tell me you are joking
Michal 'vorner' Vaner
@vorner
Sep 21 2017 18:36
I'm not. But I guess I'm not being actually nice on it. I wanted to play a bit, so I turned on quite few nightly features.
I'll try tweaking it a bit to see what causes it.
diegor8
@diegor8
Sep 21 2017 18:38
@vorner does this happen frequently ?
Michal 'vorner' Vaner
@vorner
Sep 21 2017 18:39
What do you mean, frequently? This code of mine reliably crashes the compiler.
Ok, maybe I shouldn't be using #[async] function recursively.
diegor8
@diegor8
Sep 21 2017 18:46
so , the cause is that you are using unsafe code ?
Michal 'vorner' Vaner
@vorner
Sep 21 2017 18:47
I myself don't. I guess the proc macro in futures-await crate just doesn't handle recursion very well and simply enters an infinite recursive loop itself. And the crash is well-behaved crash (with SIGABRT and an error message).
Steve Klabnik
@steveklabnik
Sep 21 2017 18:50
@diegor8 you can get a stack overflow in safe code. it will be a well-defined crash.
let x = [0u8; 100000000000000000000000000000000]; <-
TatriX
@TatriX
Sep 21 2017 18:56
I'm trying to build a windows exe from a virtual box and getting this: https://i.imgur.com/o284VCL.png
The same with 1.20.0
ashleybeshir
@ashleybeshir
Sep 21 2017 20:09
is their a way to reference a object while the object still changes it value during runtime ?
lifetime of owner and reference are the same
oyvindln
@oyvindln
Sep 21 2017 20:16
@ashleybeshir Maybe https://doc.rust-lang.org/stable/std/cell/index.html is what you are looking for?
ashleybeshir
@ashleybeshir
Sep 21 2017 20:20
@oyvindln thank you i will have a look at it
Roman Proskuryakov
@kpp
Sep 21 2017 22:22
Hello. How do I declare a static only type?
so any variable of this type must be created with static ref NAME: TYPE ?