These are chat archives for rust-lang/rust

27th
Aug 2018
Denis Lisov
@tanriol
Aug 27 2018 00:10
Use Option::cloned to get rid of the reference. If your values are Clone and you're ok with cloning them, this would be enough.
Timothy Shoaf
@timshoaf
Aug 27 2018 04:37
Hello all, I have a brief question about package structure. I am building a mid sized project that will have some shared interface code and several implementing modules that will use common code as well as specific behaviour. Ideally, I'd like the structure to have a directory for common, and one per specific implementation, where each specific implementation uses the common code. I'd like to have these all imported into a lib crate, but I would also like to have a main driver program until I get to the size necessitating true packaging. I am currently running into the following issue:
I have a src/common/mod.rs, a src/specific1/mod.rs, a src/specific2/mod.rs, a src/lib.rs and a src/main.rs
my lib.rs imports the three via pub use common / specific1 / specific2
specific1 then does a use ::common::* (yes, yes globs polluting the namespace I know)
that all seems to work... however... I can't seem to figure out how to then make the modules available in main.rs as I get no specific1 in the root type errors...
could someone help me wrap my head around the import scheme for this language? the book doesn't quite go into enough detail to demonstrate these more convoluted schemes
Timothy Shoaf
@timshoaf
Aug 27 2018 04:47
ooookay... nvm... apparently the entire application folder is the crate root as far as the lib crate is concerned so in main I have to do an extern crate myapp and an use myapp::specific1::{blah, blah, blah}
Thank you all for being my army of rubber ducks, your service was greatly appreciated :)
trsh
@trsh
Aug 27 2018 09:59
Question. For Rust we have Result, for C try/catch.. what we do in FFI. Where the result is Error or valid result (like struct)?
Michal 'vorner' Vaner
@vorner
Aug 27 2018 10:08
No, only C++ has try/catch. C has nothing like that, it signals errors by documenting what int value in the return value to use as which error, usually, or by setting values into some global variables.
And C++ doesn't have FFI into Rust.
So if you want to translate them, you have to translate Result into something (struct, etc). Push it through old-style C FFI API. Then examine it in the C++ side and throw. Or the other way around.
trsh
@trsh
Aug 27 2018 10:35
@vorner c++ is out of scope
About C, shame on me, didn't know
Santolama
@Santolama
Aug 27 2018 12:48
Hello, everyone
I'm new to learning rust
kind of drawn to it, because of its performance, stated by the documentation
I really want to explore
Restioson
@Restioson
Aug 27 2018 14:31
Is huonw here? Or is there any way I can contact them otherwise?
Lyle Mantooth
@IslandUsurper
Aug 27 2018 14:42
@Santolama, have you found https://doc.rust-lang.org/ yet? The first two links on that page are the best ways to get started, probably.
And, if you already know another programming language, searching for articles like "Rust for ___ coders" are helpful too.
Zakarum
@omni-viral
Aug 27 2018 16:13
In lot of cases it is possible to replace &T with T when T: Copy. I wonder how much performance you can get from such replacement in some generic API where Sh<'a, T> would be &'a T for !Copy types and plain T for copy types.
For example Sh<'a, &'b T> would be reduced to &'a T removing one level of indirection
While such Sh can't be implemented generically it can be implemented for any particular type with simple macro.
Avi Srivastava
@k3yavi
Aug 27 2018 20:57
Hey guys, quick question, how can one save the set intersection of two HashSets to one of the initial objects ?
let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();

// Print 2, 3 in arbitrary order.
for x in a.intersection(&b) {
    println!("{}", x);
}

// gives error since the return type is Vec<&u32>
a = a.intersection(&b).collect();
Denis Lisov
@tanriol
Aug 27 2018 21:03
@k3yavi .cloned(), probably :-)
Avi Srivastava
@k3yavi
Aug 27 2018 21:04
duh ! thanks again @tanriol