These are chat archives for rust-lang/rust

8th
Sep 2017
stevensonmt
@stevensonmt
Sep 08 2017 02:50

I'm building a string based on items in a list. I'm trying to use

list.into_iter()
.enumerate
.map(|(i,j)| mystring.push_str(&myfunc(i, j)))
.collect()

The issue is I have to specify the type for collect and I could do a vec but that's a useless structure since ultimately I just want to return mystring. Is there another way to consume the map that makes sense?

myfunc returns a string based on the list item and its index, btw.
red75prime
@red75prime
Sep 08 2017 03:15
@stevensonmt
list.into_iter()
.enumerate()
.map(|(i,j)| myfunc(i, j))
.collect::<String>()
collect uses FromIterator trait to build the result. String implements FromIterator<String>
Restioson
@Restioson
Sep 08 2017 04:29
@AlmightyFloppyFish I just find it much clearer and nicer to code, quite frankly
Again with safety and performance
Think low level, program high level
Zakarum
@omni-viral
Sep 08 2017 12:10
I have 2 libraries (A and B) that depends on 3rd (C).
B can use C of version >=0.4
A use C of version 0.5
C latest version is 0.6
How can I write Cargo.toml for B so it will use same version of C as A when compiled as dependency of A
even after A start to use C v0.6?
Aleksey Kladov
@matklad
Sep 08 2017 12:13
Cargo tries hard to unify semver compatible dependencies across the dependency graph, but it treats 0.x and 0.x+1 as incompatible.
If C 0.5 and 0.6 are really sevmver compatible, then they should probably be 0.5.1 and 0.5.2 for everything to work smoothly.
Zakarum
@omni-viral
Sep 08 2017 12:15
They are not. But the part B uses are compatible
Aleksey Kladov
@matklad
Sep 08 2017 12:15
Otherwise, you can write C = "0.4,0.5,0.6" in B's Cargo.toml
>=0.4 means 0.4.x, but not 0.5 (not 100% sure though)
Zakarum
@omni-viral
Sep 08 2017 12:16
no matching version `^0.4, ^0.5` found for package
versions found: 0.5.2, 0.5.1, 0.5.0, ...
It works with C = '*' though
But I don't like to leave '*' there
Aleksey Kladov
@matklad
Sep 08 2017 12:21
Hm, yeah, , requs are anded together... So would C=">=0.4, <=0.6" work?
Zakarum
@omni-viral
Sep 08 2017 12:24
Kind of.
If I try to compile with C = 0.6 - it fails. If I then make it C=">=0.4, <=0.6" if fails again.
But if I try to compile with C = 0.4 - it succeeds. If I then make it C=">=0.4, <=0.6" if succeeds again.
Aleksey Kladov
@matklad
Sep 08 2017 12:26
Hm, so this probably means that preexisting Cargo.lock may be the culprit. Cargo tries to minimize different versions, but it also tries to minimize Cargo.lock changes
Zakarum
@omni-viral
Sep 08 2017 12:29
It doesn't work after cargo clean
Aleksey Kladov
@matklad
Sep 08 2017 12:29
cargo clean does not remove Cargo.lock IIRC
Zakarum
@omni-viral
Sep 08 2017 12:30
huh
It doesn't work after deleting Cargo.lock
Zakarum
@omni-viral
Sep 08 2017 12:38
The only way to make it use C = 0.4 is to modify B's Cargo.toml to have C = 0.4. Build. And then make it C = ">=0.4, <=0.6 in B's Cargo.toml again
Or manually overwrite Cargo.lock
Aleksey Kladov
@matklad
Sep 08 2017 12:42
cargo update C --precise "0.4" might be better than manual rewriting
Zakarum
@omni-viral
Sep 08 2017 12:43
Maybe. But I'd like cargo to pick 0.4 automatically
Aleksey Kladov
@matklad
Sep 08 2017 12:44
I am a bit lost: how wants 0.4 exactly? A use C of version 0.5, so in theory Cargo should pick either 0.5 or 0.6...
s/how/who
StelarCF
@StelarCF
Sep 08 2017 12:44
if I'm given a Box<Any>, and I know the contents to be Arc<Mutex<GameObjectView>>, how do I transform that Box<Any> into a Box<Arc<Mutex<GameObjectView>>?
Zakarum
@omni-viral
Sep 08 2017 12:45
@matklad Whoops. I want 0.5
Aleksey Kladov
@matklad
Sep 08 2017 12:46
I've run out of advice for now :) Will try to reproduce this situation locally instead of guessing later :)
Denis Lisov
@tanriol
Sep 08 2017 12:47
@StelarCF Box::downcast?
However, if you're a given a Box<Any>, but know for sure it's a Box<Arc<Mutex<GameObjectView>>>, I's suggest you review your interfaces :-)
Zakarum
@omni-viral
Sep 08 2017 12:50
Ah. I see why it choose 0.6. There is another dependency D that use exactly C v0.6
Aleksey Kladov
@matklad
Sep 08 2017 12:50
:grinning:
This is exactly the kind of problem that public/private dependencies RFC should help to diagnose
Zakarum
@omni-viral
Sep 08 2017 12:51
Yeah. D uses C only internally
While B expose implementation of C's traits for it's public types
And A tries to use those implementations
StelarCF
@StelarCF
Sep 08 2017 12:59
@tanriol it's because of nphysics ;p
downcast helps, but I'm getting errors about moving out of borrowed content now...
                let bbb = b.borrow();
                let bb = bbb.user_data().unwrap().clone();
                let gov: Box<Arc<Mutex<GameObjectView>>> = bb.downcast().unwrap();
                                                           ^^ cannot move out of borrowed content
where b is a Rc<RefCell<RigidBody>> aka RigidBodyHandle from nphysics
Denis Lisov
@tanriol
Sep 08 2017 13:04
Or do you actually need to move it out, not just reference?
stevensonmt
@stevensonmt
Sep 08 2017 16:53
Am I correct that as_bytes() is equivalent to preceding &str with b? So
"str".as_bytes() == b"str"
StelarCF
@StelarCF
Sep 08 2017 17:16
thanks @tanriol ! that worked
StelarCF
@StelarCF
Sep 08 2017 18:52
nvm, downcast_ref returns a None....
                let bbb = b.borrow();
                let bb = bbb.user_data().unwrap().clone();
                let gov: &Box<Arc<Mutex<GameObjectView>>> = bb.downcast_ref().unwrap();
unwrap() panics
Denis Lisov
@tanriol
Sep 08 2017 18:53
Are you really sure you've got the correct type in there?
StelarCF
@StelarCF
Sep 08 2017 18:54
well that's what I put into it..
I think
            rb.set_user_data(Some(Box::new(gov)));
where gov is
gov: Arc<Mutex<GameObjectView>>
Denis Lisov
@tanriol
Sep 08 2017 18:55
BTW, why do you need .clone() in the bb line?
StelarCF
@StelarCF
Sep 08 2017 18:56
I don't know,, I thought it'd get rid of the borrow issue..
Denis Lisov
@tanriol
Sep 08 2017 18:56
Does it work like this?
let gov: &Arc<Mutex<GameObjectView>> = bb.downcast_ref().unwrap();
StelarCF
@StelarCF
Sep 08 2017 18:57
trying it out
works :D
Robert Peters
@r2p2
Sep 08 2017 19:01
Is it correct, that each file (except main.rs and lib.rs) is its own module and that therefore a module cannot span multiple files?
Denis Lisov
@tanriol
Sep 08 2017 19:03
Does not span multiple files. Well, technically there's include!, but you probably don't want that...
Robert Peters
@r2p2
Sep 08 2017 19:11
I see. I am used to put classes in their own files while files within the same directory share the same namespace. (thats my structure when coding c++) Is it still valid to associate classes with structs and namespaces with modules or is there a different meaning behind the rust way of thinking about those terms? Don't know if my question makes sense. :D
Moggers
@Moggers
Sep 08 2017 19:28
if I have two modules, in a project, can I use one from the other?
Aleksey Kladov
@matklad
Sep 08 2017 19:28
yep
you can even have cyclic deps between modules
Moggers
@Moggers
Sep 08 2017 19:29
hm
oh!
so ALL modules need to be defined in main.rs, not only the ones which are used in main.rs?
Aleksey Kladov
@matklad
Sep 08 2017 19:34
Modules for a tree
each module except the root one must be defined in the parent
so you may define foo and bar in main
and then baz and quux in foo
Moggers
@Moggers
Sep 08 2017 19:34
so if I have main which has the children x and y, and y has the children w and g
and I want to use g from x
Aleksey Kladov
@matklad
Sep 08 2017 19:35
you can write ::y::g
or super::y::g.
Moggers
@Moggers
Sep 08 2017 19:35
I have to mod g and w in y, mod y in main, and then use y::g in x?
Aleksey Kladov
@matklad
Sep 08 2017 19:35
Leading :: means the root module, super means the parent module
Moggers
@Moggers
Sep 08 2017 19:35
oh I thought things were implied to be from the root hm
okay
thanks
Aleksey Kladov
@matklad
Sep 08 2017 19:35
Yep, you are correct!
Keep in mind though that paths in use implicitly start with ::
Moggers
@Moggers
Sep 08 2017 19:36
somehow the compiler is asking me for y::g::g::function now O.O
Aleksey Kladov
@matklad
Sep 08 2017 19:36
that is, use y::g is the same as use ::y::g::.
Moggers
@Moggers
Sep 08 2017 19:36
oh yeah I thought that was the case, implicitly starting with ::
okay all fixed
yay
Aleksey Kladov
@matklad
Sep 08 2017 19:37
Yeah, and if you use y::g
then inside the module you write g::function
that is, use foo::bar::baz brings the baz name into the scope.
stevensonmt
@stevensonmt
Sep 08 2017 21:11
what's the advantage of storing a bitvec versus a vec of booleans?
Denis Lisov
@tanriol
Sep 08 2017 21:13
More compact, may be faster to process...