These are chat archives for rust-lang/rust

31st
Mar 2019
Sam Johnson
@sam0x17
Mar 31 03:38
how do I include a sibling module?
Brian Knapp
@knappador
Mar 31 03:40
@sam0x17 describe sibling
Sam Johnson
@sam0x17
Mar 31 03:41
as in, I have src/some_module.rs and I have src/some_other_module.rs and I want to include some_other_module in some_module
and by include I mean make use of
and to complicate things, some_module has submodules aka src/some_module/something.rs -- those all can be used just fine, I just can't make use of a sibling
I have tried doing use some_other_module; but that doesn't work, I've also tried mod some_other_module; use some_other_module; also doesn't work
Brian Knapp
@knappador
Mar 31 03:42
Gotcha. use crate::some_module::something::download_more_pron;
Sam Johnson
@sam0x17
Mar 31 03:43
replace crate with name of crate, or literally crate?
Brian Knapp
@knappador
Mar 31 03:43
literally crate
Sam Johnson
@sam0x17
Mar 31 03:43
ahhhh
ok I'll try it
Brian Knapp
@knappador
Mar 31 03:44
And might try mod some_module; in your main.rs if it says they aren't in the crate yet. I did not have time to get good at this but it works reasonably so far
Sam Johnson
@sam0x17
Mar 31 03:44
no [module name here] in the root :/
ok I'll try that
adding mod [my module]; to lib.rs worked :D
so am I understanding correctly that you have to basically forward declare everything that is in the current crate with mod statements in your lib.rs, and then everything magically works when you go to use?
I have yet to be able to set up a module without having to consult gitter lolol
Brian Knapp
@knappador
Mar 31 03:50
Beats me. I got lucky and began learning other things first.
Sam Johnson
@sam0x17
Mar 31 04:15
lolz
Riley Cat
@WreckedAvent
Mar 31 05:36
think of mod as telling the compiler what files to reference and use as creating shortcuts through the name space
if something is referenced already (such as in lib or main) then you don't need to tell the compiler to reference it again - it's already doing it
this allows you to just have one file that references everything else and it "just works" for sibling modules
but more complicated modules can still have their own mods that the main entry point doesn't know about
Riley Cat
@WreckedAvent
Mar 31 05:45
it's an interesting middle ground between project files that define every single possible file to compile and stand alone
where any given file can be compiled on its own since it includes everything it needs
Sam Johnson
@sam0x17
Mar 31 06:16
if I have something like let limits = [1,3,5,10,14,13,17,27,22,24,47,2,4,20,25,1000,3007]; is there a compact way of saying those are all u64's
I don't want to do [1u63, 2u64.. etc
Riley Cat
@WreckedAvent
Mar 31 06:19
let limits: [u64; 17] = [ ... ];
the way type inference works you can also pass it to a function which defines the type and rust will figure it out
Sam Johnson
@sam0x17
Mar 31 06:27
ahhh kk thx @WreckedAvent
I'm guessing there is no way to omit the size and have the compiler figure that out?
sam0x17 @sam0x17 begins counting with arrow keys
Riley Cat
@WreckedAvent
Mar 31 06:28
arrays of different size are of different type
Sam Johnson
@sam0x17
Mar 31 06:29
yeah I just mean theoretically the compiler could do something like let limits: [u64, _] = [thing of fixed size] and auto fill the _
Riley Cat
@WreckedAvent
Mar 31 06:30
I don't think so
I typically use vec or something when I don't care about the size
Sam Johnson
@sam0x17
Mar 31 06:31
gotcha, yeah this is one of those like I have a fixed list of things (should be a constant really) but I don't care to count them
Riley Cat
@WreckedAvent
Mar 31 06:31
the _ placeholder is mostly for generic inference afaik
like in Vec<_>
Sam Johnson
@sam0x17
Mar 31 06:33
yup, would be convenient for something like this, but yeah there are other things way more important like implementing vec/array/slice equality beyond 32 elements
I ran into that the other day and it took me an hour to realize it just isn't implemented beyond 32 elements -- kept thinking there was some difference in my code between these two places but turned out just to be the number of elements
I'm guessing that's one of those tradeoffs where they have a macro literally generating all permutations of those equality definitions, and they don't want to generate too many
Riley Cat
@WreckedAvent
Mar 31 06:37
Sam Johnson
@sam0x17
Mar 31 06:37
hahahaha omg yes
I first learned in vb.net in 2005 and yes
Riley Cat
@WreckedAvent
Mar 31 06:39
really generic code is such a pain without automatic generalization
and I know why rust doesn't have it but still
Sam Johnson
@sam0x17
Mar 31 06:47
maybe you could define some sort of macro that gets called at the top of the current file that will generate any needed equality implementations for the current file, and then that could potentially be included in the standard prelude -- in principle you would just have to iterate over all array/slice/vec types in the file, and then generate equality implementations for just the ones that appear there.. would be leaner then the current approach in theory
Riley Cat
@WreckedAvent
Mar 31 06:48
use macros for DSLs, not a hack around language features you don't have
Sam Johnson
@sam0x17
Mar 31 06:48
its all easily doable with static analysis so unless rust macros are limited in some way that makes this impossible I don't see why you couldn't do it
defining thousands of generic implementations sounds hackier to me -- I bet they use a macro to generate them too
if a language allows [u64; 18719879847] as a type, and I have two of those, == should just work, it's silly that it doesn't
but then I think the fact that hashing doesn't just work out of the box for all types is silly too so I'm clearly at odds with the languages design intentions
Riley Cat
@WreckedAvent
Mar 31 06:53
I suspect it has something to do with how the compiler will unwind loops and the language guiding you towards not doing the dumb thing like comparing millions of values by value
which is almost certainly the wrong solution
Sam Johnson
@sam0x17
Mar 31 06:56
or that they had to write the compiler in rust XD
lol but no that makes sense
Riley Cat
@WreckedAvent
Mar 31 06:57
it's like with the c# example, they only defined up to a sixteen tuple argument for lamdas
but like
if you need more
your solution is probably wrong dummkopf
Sam Johnson
@sam0x17
Mar 31 07:00
definitely true for Actions in C# but for static arrays anthing is possible and legitimate e.g. if you have a huge precomputed table that you want baked into your binary
Riley Cat
@WreckedAvent
Mar 31 07:02
I mean theoretically sure but how often would you need structural equality on such a thing
but anyway I'm just some random person on the internet and I'm sure the relevant RFC went through all the details
Denis Lisov
@tanriol
Mar 31 14:32
One useful trick with arrays is that you always can take a whole-array slice &arr[..], and these are not limited to 32 elements. But hopefully const generics will fix the problem with arrays.
Björn Harrtell
@bjornharrtell
Mar 31 19:23
I'm still a Rust newbie and trying to learn things by writing a REST API using Rocket. Going reasonably well, but I get a compiler error I do not understand when trying to make a route return a Result with Json as both Ok and Err. The message is along the lines of "the trait rocket::response::Responder<'_> is not implemented for"... but it should be for the Json type so I don't understand it. Minimal code example at https://gist.github.com/bjornharrtell/5ea10f0003378bf76d8f2abe04079e2a.
Denis Lisov
@tanriol
Mar 31 19:34
@bjornharrtell The JsonError struct is missing a Debug impl. If you make it #[derive(Debug, Serialize)], everything works.
Björn Harrtell
@bjornharrtell
Mar 31 19:37
Thanks @tanriol - it was not clear to me. But now I know how to read type bounds and it says Responder<'r> + Debug in this specific case. :)
Brian Knapp
@knappador
Mar 31 22:36
Are there usually differences between linking during test & build? I have some link errors when building tests.
static libs are involved
Denis Lisov
@tanriol
Mar 31 22:36
What's the error?