These are chat archives for rust-lang/rust

13th
Dec 2017
James McCoy
@jamessan
Dec 13 2017 03:22
Is this a different issue than rust-lang/rust#42748 ? Rust clearly sees that cond is being mutated (dropping the mut fails to compile) but with mut warns that the value isn't being read
Sean
@seanr707
Dec 13 2017 04:18
@jamessan It's not being read for any use. It is changed before the initial value is read. The compiler is essentially wondering why you're doing
let mut b = false;
// Nothing done with this first false value
{
  b = 5 < 3; // false
}

// when instead we could just type

let b = 5 < 3;
@mnivoliez Try making a temp variable. let slice = nodes.as_slice(); to use
James McCoy
@jamessan
Dec 13 2017 04:22
My actual code is more complex. I need the extra scope
That was just a minimal example
Sean
@seanr707
Dec 13 2017 04:24
@jamessan Do you have a link to the source or able to paste the larger snippet?
James McCoy
@jamessan
Dec 13 2017 04:40
ah, I can get rid of the warning if I just use let cond; instead of let mut cond = false;
https://gist.github.com/f03a4aa626d0109bd1b0f3d6372d18f1 is closer to the actual code. I can also avoid the extra scope by changing let x = map.entry(word).or_insert(0); to let x = *map.entry(word).or_insert(0);
Roman Proskuryakov
@kpp
Dec 13 2017 09:09
let cond = {
   let x = map.entry(word).or_insert(0);
   *x < 3
};
or
Roman Proskuryakov
@kpp
Dec 13 2017 09:14
let cond = map.entry(word).or_insert(0).cmp(3) == Ordering::Less
smt like that
red75prime
@red75prime
Dec 13 2017 09:27
let cond = *map.entry(word).or_insert(0) < 3;
Roman Proskuryakov
@kpp
Dec 13 2017 09:31
)))
James McCoy
@jamessan
Dec 13 2017 12:02
There's actually a match involved, so it's not simple enough to boil down to a single condition, but I like the let cond = { ... }; solution.
Thanks
Haven't internalized that you can do stuff like that, yet
Sherab Giovannini
@Shaddy
Dec 13 2017 12:36
@snarf95_twitter that's right xDDD
Sean
@seanr707
Dec 13 2017 14:42
I love that variable assignments are expressions on the right hand stead instead of statements.That is definitely one of my favorite things about Rust. (Think I described that correctly)
Restioson
@Restioson
Dec 13 2017 15:26
Hmmm
So I'm stuck on an unconstrained type parameter error --
impl <E, T: Trait<E>> fmt::Write for T
apparently E is unconstrained
Restioson
@Restioson
Dec 13 2017 15:31
Ok apparently that whole thing isn't legal :/
Hans W. Uhlig
@huhlig
Dec 13 2017 15:31
the cops are coming to get you
Restioson
@Restioson
Dec 13 2017 15:32
Hah, our cops are incompetent
Hans W. Uhlig
@huhlig
Dec 13 2017 15:32
lucky, ours are brutal and negligent
Restioson
@Restioson
Dec 13 2017 15:33
:/ sometimes our cops get it together enough to arrange a massacre of some protestors
Hans W. Uhlig
@huhlig
Dec 13 2017 15:33
yeah... they've done that a few times
usually they just target the minorities and lock them up in endentured servitude
mnivoliez
@mnivoliez
Dec 13 2017 16:34
@seanr707 It does not work. But I was able to find that the lifetime problem reside in the constructor of Mainstate:
error[E0597]: `nodes` does not live long enough
  --> src/main.rs:77:27
   |
77 |         let nodes_slice = nodes.as_slice();
   |                           ^^^^^ does not live long enough
...
86 |     }
   |     - borrowed value only lives until here
   |
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 75:5...
  --> src/main.rs:75:5
   |
75 | /     fn new(ctx: &mut Context) -> GameResult<MainState> {
76 | |         let nodes = generate_nodes(50, GRID_X, GRID_Y);
77 | |         let nodes_slice = nodes.as_slice();
78 | |         let edges = generate_edges_from_nodes(nodes_slice);
...  |
85 | |         Ok(s)
86 | |     }
impl<'a> MainState<'a> {
    fn new(ctx: &mut Context) -> GameResult<MainState> {
        let nodes = generate_nodes(50, GRID_X, GRID_Y);
        let nodes_slice = nodes.as_slice();
        let edges = generate_edges_from_nodes(nodes_slice);
        let s = MainState {
            generation: 0,
            nodes: nodes,
            edges: edges,
            paths: Vec::new(),
        };
        Ok(s)
    }
}
Denis Lisov
@tanriol
Dec 13 2017 16:52
@mnivoliez Does the edges field reference nodes?
mnivoliez
@mnivoliez
Dec 13 2017 16:53
The edges field is construct witht the nodes ans should references them.
Denis Lisov
@tanriol
Dec 13 2017 16:57
Normal Rust structures cannot contain both some owned data and a reference to it. This is a known limitation :-(
mnivoliez
@mnivoliez
Dec 13 2017 17:34
should I use Rc then?
mnivoliez
@mnivoliez
Dec 13 2017 18:14
This one I mean Rc
mnivoliez
@mnivoliez
Dec 13 2017 18:20
I could use another approache
Zakarum
@omni-viral
Dec 13 2017 19:53
Why does Cow<T> not implement From<&T> and From<T::Owned>. For all T: ToOwned? Instead it implements those for particular types, like str, Path and [T]
Denis Lisov
@tanriol
Dec 13 2017 19:59
I don't see any fundamental reason; may be simply an oversight.
Alexander Ronald Altman
@pthariensflame
Dec 13 2017 20:05
I tried to replace them with such an instance at one point, but it was rejected in favor of me adding additional specialized ones. That was a while ago, though…
Oh, wait, never mind.
I misremembered which instances I had added.
rust-lang/rust#35064
Similar, though.