These are chat archives for rust-lang/rust

18th
Jul 2018
tsoernes
@tsoernes
Jul 18 2018 14:32
Is there such a thing as a mutable reference, in the sense that what it points to can be changed, but not the thing itself?
Hans W. Uhlig
@huhlig
Jul 18 2018 14:33
final vs const you mean?
Not in rust that im aware of
tsoernes
@tsoernes
Jul 18 2018 14:33
I don't think so. Imagine a 'mutable reference' to a list. I want the list to be static/non-mutable: you can't add, remove, or change elements
But you might change the 'mutable reference' to point to another list
Dmitriy
@dpogretskiy
Jul 18 2018 14:34
and then what happens to the data that was there before?
Hans W. Uhlig
@huhlig
Jul 18 2018 14:34
logically nothing
it's a pointer to someone elses borrowed data
tsoernes
@tsoernes
Jul 18 2018 14:35
Yes, someone else owns the list
Dmitriy
@dpogretskiy
Jul 18 2018 14:35
so it's an Arc?
Hans W. Uhlig
@huhlig
Jul 18 2018 14:35
logically I assume it's a pointer to an rc
Denis Lisov
@tanriol
Jul 18 2018 14:35
@tsoernes Like here?
Dmitriy
@dpogretskiy
Jul 18 2018 14:35
but with reference, that can be changed?
@tanriol :+1: good one
Adler Oliveira
@adleroliveira
Jul 18 2018 16:16
Hello all, quick question: whats the best way to go from Vec<u8> to Vec<usize> ? maybe iter().map()?
Dmitriy
@dpogretskiy
Jul 18 2018 16:17
something like that
and then collect
Adler Oliveira
@adleroliveira
Jul 18 2018 16:18
hmm thought there were a "cleaner" way
Dmitriy
@dpogretskiy
Jul 18 2018 16:21
it's not your every day task to make a Vec of usizes of u8s :)
it's also somewhat slowish to really do
you allocate, reallocate and so on
Adler Oliveira
@adleroliveira
Jul 18 2018 16:22
I have a vec of "integers" that are no higher than 255 so they are "packed" as u8. And I need to convert it back to u32 (or usize for that matter)
Dmitriy
@dpogretskiy
Jul 18 2018 16:23
you can cast them to usize/u32 whrere you need them to be such
unless it's set in stone, that no other thing than Vec<usize> comes in
Adler Oliveira
@adleroliveira
Jul 18 2018 16:23
do you know if serde serialize Vec<u8> to json as an array of integers?
Lyle Mantooth
@IslandUsurper
Jul 18 2018 16:23
It does.
Dmitriy
@dpogretskiy
Jul 18 2018 16:23
i think it could
Adler Oliveira
@adleroliveira
Jul 18 2018 16:24
ok because this is what I need. to serialize Vec<u8> as individual integers in an array using serde
thanks, I'm going to test it
c0nk
@c0nk
Jul 18 2018 17:08
Is this the right place to get help with rust?
Denis Lisov
@tanriol
Jul 18 2018 17:13
One of possible places :-)
Adler Oliveira
@adleroliveira
Jul 18 2018 17:19
I will gladly help if I can
Nopey Nope
@Nopey
Jul 18 2018 17:24
Is there a way to replace a substrings in-place with a same-size string, like str.replace w/o alloc? I know its easy enough to make, but if its in std:: that'd be ideal :ok_hand:
Lyle Mantooth
@IslandUsurper
Jul 18 2018 17:29
@Nopey str::replace()
Oh wait. Not in place.
@Nopey String::replace_range() should.
And doesn't need to be the same size, either.
c0nk
@c0nk
Jul 18 2018 18:53
Should I be writing a new function like: fn new() -> Self ? Seems better to me to use 'Self' and not rewrite the type for the return type, but the examples in the standard library that ive seen don't do this. Why not?
Andrey Lesnikov
@ozkriff
Jul 18 2018 19:03
i'm using Self always. clippy even has a lint about this. standard library docs - not sure, but they may have been written before Self alias was introduced to the language
c0nk
@c0nk
Jul 18 2018 19:04
rust by example docs don't do it either
Ichoran
@Ichoran
Jul 18 2018 19:55
I also prefer Self unless I might somehow lose track of which type it's for, in which case naming it helps.
(Theoretically--it's probably bad practice to have so much code around a new that you can't easily tell who it belongs to. I haven't yet.)
David Raifaizen
@craftytrickster
Jul 18 2018 19:58
Ive been getting an error - expected fn pointer, found fn item
I suspect it is because I am trying to place a generic function inside of a closure, is that the cause?

if so, how do you indicate the generic type of a function that you want to pass into , for example

thing
.and_then(do_generic_thing)

tsoernes
@tsoernes
Jul 18 2018 21:41
Say I want to convert a hashmap to a vector in some fashion. Is that possible without copying? If you iter_mut through the hashmap, is there a way to 'move out of borrowed content' if you know the hashmap won't be used afterwards?
Denis Lisov
@tanriol
Jul 18 2018 21:47
@tsoernes HashMap implements IntoIterator, so you can iter its items by move.
let key_value_pairs: Vec<_> = hash_map.into_iter().collect();
tsoernes
@tsoernes
Jul 18 2018 21:48
excellent, thanks

any hints for this one?

let ingrs: Vec<Vec<String>> = ...
ingrs.sort_unstable_by_key(|li| li[0]);
 // cannot move out of borrowed content

Do you really need to copy the inner string for use as key when sorting?

Denis Lisov
@tanriol
Jul 18 2018 21:54
Does |li| &li[0] work?
tsoernes
@tsoernes
Jul 18 2018 21:56
cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
the String needs to live long enough for the comparison, although that cannot be guaranteed apparantly
Denis Lisov
@tanriol
Jul 18 2018 22:00
Ok, then ingrs.sort_unstable_by(|left, right| left[0].cmp(&right[0]));