These are chat archives for rust-lang/rust

6th
Sep 2017
Restioson
@Restioson
Sep 06 2017 05:29
So - cargo has build scripts. Can I write an install script for my binary crate? W
Andrey Lesnikov
@ozkriff
Sep 06 2017 06:13
rust-lang/cargo#545 - nope (yet?)
but cargo build is not meant to be a general purpose build system for these sorts of applications.
Sergey Noskov
@Albibek
Sep 06 2017 06:30
@Restioson there are distribution package (like deb/rpm) plugins in the wild though
stevensonmt
@stevensonmt
Sep 06 2017 14:21
To initialize an array of size 10 with a default value I can do let arr = ["default", 10]. But what if I need to calculate the size from a variable and then initialize the array. Something like
let var_size = x*3;
let arr = ["default", size];
That gives me the error "non-constant used with constant"
stevensonmt
@stevensonmt
Sep 06 2017 14:26
So even though I'm not going to grow the vec after initialization I have to use vec not array?
Hans W. Uhlig
@huhlig
Sep 06 2017 14:27
rust doesn't like dynamicly sized stacks
stevensonmt
@stevensonmt
Sep 06 2017 14:27
ok, thanks
Denis Lisov
@tanriol
Sep 06 2017 14:28
There's an RFC that will allow this in some cases: rust-lang/rfcs#1909
Hans W. Uhlig
@huhlig
Sep 06 2017 14:29
I still don't understand why rust doesn't like DSTs on the stack but it doesn't
stevensonmt
@stevensonmt
Sep 06 2017 16:16

Can someone tell me why my while loop does not exit once the counter reaches the limit?

    let mut prime_x = 1;
    let mut prime_count = 0;
    while prime_count < x as u64 {
        for i in 2..upper_limit {
            match prime_indices[i] {
                true => { prime_count += 1;
                        prime_x = i},
                false => prime_x = prime_x,
            }
        }

upper_limit is calculated from the parameter x as x(log(xlogx))

It seems to exit properly up to x = 12 for some reason.
stevensonmt
@stevensonmt
Sep 06 2017 16:44
Still don't know why the while loop wasn't working, but worked around it by just putting a conditional break in the for loop.
Hans W. Uhlig
@huhlig
Sep 06 2017 17:36
err is it beacuse it must finish the for loop first?
stevensonmt
@stevensonmt
Sep 06 2017 17:37
why doesn't the while condition break that loop when met though?
Hans W. Uhlig
@huhlig
Sep 06 2017 17:37
because the foor loop must exit before the conditional is tried
stevensonmt
@stevensonmt
Sep 06 2017 17:38
got it
Hans W. Uhlig
@huhlig
Sep 06 2017 17:38
so if your while loop conditional is met in the middle of the for loop but the for loop conditional isn't met
then the for loop will complete prior to the while loop conditional being retested
make sense?
stevensonmt
@stevensonmt
Sep 06 2017 17:39
it does. not sure why i was thinking the while condition would be monitored during the for loop.
thanks for clarifying.
Hans W. Uhlig
@huhlig
Sep 06 2017 17:39
heh
technically if you wanted it to exit, you want the for conditional and the while conditional to be the same conditional such that both are tested on each iteration
not quite sure how to properly structure that in rust though
my crash coursing rust has ended in lots of crashing and even more questioning of design decisions
stevensonmt
@stevensonmt
Sep 06 2017 17:41
    let mut prime_x = 1;
    let mut prime_count = 0;
    for i in 2..upper_limit {
        if prime_indices[i] {
            prime_count += 1;
            prime_x = i
        }
        if prime_count == x as u64 {
            break
        }
    }
That's what I ended up with and it works but certainly not elegant.
Hans W. Uhlig
@huhlig
Sep 06 2017 17:42
eh, I've found that rarely are low level calculations particularly "elegant"
you usually need a level of abstraction or three before things just look pretty
Denis Lisov
@tanriol
Sep 06 2017 17:44
How about something like the following?
prime_indices.iter()
    .enumerate()
    .filter(|(i, &p)| p)
    .map(|(i, _)| i)
    .nth(x-1)
stevensonmt
@stevensonmt
Sep 06 2017 18:04
I sort of get it until the last call to nth(x-1). what is the nth function?
Denis Lisov
@tanriol
Sep 06 2017 18:10
Iterator::nth returns the nth element (starting with 0)
Almost the same thing [x-1] would do except that you can't use [n] with iterators (it's a method to highlight it's not O(1) and actually consumes the iterator)
stevensonmt
@stevensonmt
Sep 06 2017 18:13
ah, that's handy. Thanks.
stevensonmt
@stevensonmt
Sep 06 2017 18:30
Hmm, not working for me due to issues with filter and map. No time to troubleshoot it now but I'll look into it this evening. Thanks for the tips @tanriol
Denis Lisov
@tanriol
Sep 06 2017 18:38
Sorry, it has to be .filter(|&(i, &p)| p) - I'm rather bad at compiler's job :-)
Hans W. Uhlig
@huhlig
Sep 06 2017 18:40
ok, so here is a design question
say I have a struct, and I want to render it to screen and I need to give it a render method
if I was to do this in hierarcical form I would subclass it, add a backbuffer and render method
in a compositional form I would create a secondary renderer class with a reference to the map and the buffer and then friend the map to gain access to it's internals
however in rust you cant have friends across modules
and trait implementations can't have data
what's considered the appropriate method?
Denis Lisov
@tanriol
Sep 06 2017 18:52

however in rust you cant have friends across modules

IIUC, you can do that with some pub(restricted) magic...

Hans W. Uhlig
@huhlig
Sep 06 2017 19:48
hmm... is using factory pattern considered an anti-pattern in rust?