These are chat archives for rust-lang/rust

30th
Aug 2017
Zakarum
@omni-viral
Aug 30 2017 11:29
Hi, everyone!
I have a question :smile:
Aleksey Kladov
@matklad
Aug 30 2017 11:31
@omni-viral yep?
Zakarum
@omni-viral
Aug 30 2017 11:34

Suppose I have following traits and a function

trait ByRef<'a> {
    type Ref: 'a;
    fn by_ref(&'a self) -> Self::Ref;
}

trait ComplexTrait<'a, T> {}

impl<'a, X, T, R> ComplexTrait<'a, T> for SomeType<X>
    where T: ByRef<'a, Ref=R>,
          R: AnotherTrait<X>
{
    ...
}

fn foo<X>(&self)
    where SomeType<X>: for<'a> ComplexType<'a>
{
    ...
}

Here compiler complains that it can't infer R for Ref=R in for<'a> ComplexType<'a>

I really have no idea what this error means :smile:
Aleksey Kladov
@matklad
Aug 30 2017 11:36
Hm, a playground example might be useful here: it's hard to tell what SomeType or ComplexTypes are...
Zakarum
@omni-viral
Aug 30 2017 11:37
I'll try to make an example there
Hmmm. Funny thing. I've changed surrounding code alot since I've got the error and... it isn't reproducing anymore :laughing:
Thank you for help :+1: @matklad
Zakarum
@omni-viral
Aug 30 2017 11:42
Ah. I've got it again :frowning:
Aleksey Kladov
@matklad
Aug 30 2017 11:54
Hm, I have no idea what's going on there... I would guess that where T: ByRef<'a, Ref=R>, part is problematic, because 'a is for qualified at call site, so this bound must hold for any A for the same T which seems tough.
Zakarum
@omni-viral
Aug 30 2017 12:03
And it is holds
But with different Refs
But all Refs implements AnotherTrait
Paul Daniel Faria
@Nashenas88
Aug 30 2017 12:21
I’ve never actually used a where clause on a struct before
That part is throwing me off, and I’m not sure what the compiler does in that case.
Zakarum
@omni-viral
Aug 30 2017 12:26
@Nashenas88 if you're talking about my case there is no where clauses on structures.
Paul Daniel Faria
@Nashenas88
Aug 30 2017 12:27
I’m talking about
struct SomeType<X>(X);

fn foo<T, X>(_: SomeType<X>, _: T) -> usize
where SomeType<X>: for<'a> ComplexTrait<'a, T>,
Zakarum
@omni-viral
Aug 30 2017 12:28
Paul Daniel Faria
@Nashenas88
Aug 30 2017 12:30
I see. That makes the T stand out for me though. At this call, how are you binding T to ’a’? Could that be part of the issue? foo doesn’t specify the relationship here, but there is one in ComplexTrait? I’m really stumped by this one.
I’ve got to head to work. I hope you can figure this one out
Zakarum
@omni-viral
Aug 30 2017 12:33
Inside the function I call T::by_ref() to get ther reference. That's where 'aget specified. But as it is inside the function I can't place it at function definition level foo<'a, ...>
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:33
I'm wondering, why doesn't this code compile? (in the sense why is the for loop defined that it always returns (), instead of the value of the last iteration)
  let x = for i in 1 .. 10 {
      i
  };
Zakarum
@omni-viral
Aug 30 2017 12:34
Where should values of previous iterations go?
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:35
Gone, dropped.
Restioson
@Restioson
Aug 30 2017 12:35
List comprehension would be nice
(slice comprehension?)
Zakarum
@omni-viral
Aug 30 2017 12:36
I think I saw an RFC for break keyword to be able to return a value from loop
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:36
@Restioson That would be… problematic due to dynamic sizes of the slice.
Zakarum
@omni-viral
Aug 30 2017 12:36
In let x = for i in 1 .. 10 { break i; };
x becomes 1
Restioson
@Restioson
Aug 30 2017 12:37
@vorner couldn't the size be worked out at compile time?
red75prime
@red75prime
Aug 30 2017 12:37
@vorner
let x;
for i in 1 .. 10 {
      x = i;
};
Restioson
@Restioson
Aug 30 2017 12:38
in some cases it could... others, not
Zakarum
@omni-viral
Aug 30 2017 12:38
let mut x;
for i in 1 .. 10 {
      x = i;
};
then
Restioson
@Restioson
Aug 30 2017 12:39
could force the iterated thing to have a constant size
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:39
Yes, well, having the value of the last iteration if I do not break would be nicely consistent with if cond { a } else { b } and such things
Restioson
@Restioson
Aug 30 2017 12:40
The trouble is that it's run more than once
So it's not the "last" thing anymore
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:40
@Restioson I think it would make more sense to maybe return an iterator. Something like a map. But then, you could take that iterator and carry it somewhere else without having any closure to take with you, which is… strange.
red75prime
@red75prime
Aug 30 2017 12:40
@vorner What to do with a cycle that runs zero iterations?
Zakarum
@omni-viral
Aug 30 2017 12:41
@red75prime panic! :laughing:
or Default::default
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:41
@red75prime This was probably the answer I was looking for. Now it makes sense why it doesn't work.
Just like if without else.
Restioson
@Restioson
Aug 30 2017 12:41
Compile time error?
Zakarum
@omni-viral
Aug 30 2017 12:41
there is for...else in python. We can have it here too
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:42
@Restioson Sometimes you can't really decide if you will get some iterations for sure or not and it would probably be too complex with little benefit.
Restioson
@Restioson
Aug 30 2017 12:42
Mm

I think I misunderstood -- what do you mean by

a cycle that runs zero iterations

?

Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:43
No, I think that the problem with 0 iterations might have been the actual reason and explains it.
Zakarum
@omni-viral
Aug 30 2017 12:43
In let z = if cond { x } z may be Option<type-of-x>
with Some(x) or None
Restioson
@Restioson
Aug 30 2017 12:43
Do you not have the same type of error here?
let x;
for i in 1 .. 10 {
      x = i;
};
If the for never runs you're accessing uninitialized memory
Zakarum
@omni-viral
Aug 30 2017 12:44
@Restioson try it with 0..0
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:44

Well, if you have this:

let a = for i in 0..x {
  i
};

Then what is inside a if x was 0 to begin with

@Restioson Actually, you get the error about possibly uninitialized variable, and you need to start with let mut x = Default::default() or something.
Zakarum
@omni-viral
Aug 30 2017 12:45

This

fn main() {
    let mut x;
    for i in 1..1 {
        x = 1;
    };
    println!("{}", x);
}

give me an error

Restioson
@Restioson
Aug 30 2017 12:46
Yeah, that's what I mean -- can't you get the same error for a loop that never runs?
The logic is similar isn't it?
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:46
(I actually did write the let x = for code, failed compilation, went through that let mut x; thing, got another error and went for the last one, so my question wasn't fully academical)
Restioson
@Restioson
Aug 30 2017 12:46
I mean, the one can almost compile to the other, except x has to be mut

can't you get the same error for a loop that never runs?

sorry, meant with the let x = for ... scenario loop

lemonxah
@lemonxah
Aug 30 2017 12:48
but it doesn't know if the loop will run or not
for i in 1 .. 1 { ... } doesn't run
Restioson
@Restioson
Aug 30 2017 12:48
The compiler does know that
Zakarum
@omni-viral
Aug 30 2017 12:48
@Restioson Compiler pretends taht it isn't
Michal 'vorner' Vaner
@vorner
Aug 30 2017 12:49
The compiler may sometimes know, in simple cases. But if you wanted it to know sometimes, you'd have to define the rules about what sometimes means. Which is probably too much work for very little gain.
lemonxah
@lemonxah
Aug 30 2017 12:50
its because you didn't initialize the variable and only setting it in a control structure which might or might not happen and the error says that "use of possibly uninitialized 'x'"
Restioson
@Restioson
Aug 30 2017 12:50
Even if it can't for the let x = for thing, you can just compile that to something like
let x = {
    let mut x_current;
    for i in 0..10 {
        x_current = i;
    }
    x_current
}
lemonxah
@lemonxah
Aug 30 2017 12:51
yes because you are not using the x_current anywhere
Restioson
@Restioson
Aug 30 2017 12:51
(edited, sorry)
lemonxah
@lemonxah
Aug 30 2017 12:52
that doesn't work
try compiling it again
Restioson
@Restioson
Aug 30 2017 12:55
just wrote it here, will try in playground
lemonxah
@lemonxah
Aug 30 2017 12:56
its the same exact issue its uninitialized
Restioson
@Restioson
Aug 30 2017 12:57
hm, I see what you mean
David Harvey-Macaulay
@alteous
Aug 30 2017 15:14
Does anyone know the current status of compiling Rust to web assembly?
I've heard Emscripten is broken with Rust lately.
Can someone help me with this immutable - mutable borrow conflict?
stevensonmt
@stevensonmt
Aug 30 2017 17:16
Sorry, doesn't look any different at your link @max-frai
Max Frai
@max-frai
Aug 30 2017 17:17
last() returns option with reference. You could dereference it with *, you have integer in vector so it clones into last_item
let last_item = *my_vec.last().unwrap();
Star added here before my_vec
stevensonmt
@stevensonmt
Aug 30 2017 17:20
Thank you so much.
Max Frai
@max-frai
Aug 30 2017 17:21

@stevensonmt Maybe you will understand better, instead of * you can call directly clone:

let last_item = my_vec.last().unwrap().clone();

And this removes usage of reference returned by last() :)
stevensonmt
@stevensonmt
Aug 30 2017 17:57
Makes sense. I had tried calling my_vec.clone().last().unwrap() because I misunderstood the error. Thanks for the clarification.
Flavio Oliveira
@wisespace-io
Aug 30 2017 18:50
I am struggling to transform the string below in a date, I always get "BadFormat" error ...
let dt1 = DateTime::parse_from_str("2017-08-30 18:34:06.638997932 UTC", "%Y-%m-%d %H:%M:%S.%9f");
Denis Lisov
@tanriol
Aug 30 2017 20:13
@wisespace-io There are several problems here, including the unneeded 9 in %9f and the fact Chrono does not parse timesone names...
Flavio Oliveira
@wisespace-io
Aug 30 2017 20:28
@tanriol I see. I solved .. thank you
David Harvey-Macaulay
@alteous
Aug 30 2017 20:38
Is it possible to rename a crate on crates.io?
James McCoy
@jamessan
Aug 30 2017 21:28
based on http://doc.crates.io/crates-io.html, it sounds like you would start publishing new versions of the crate under the new name. You can't remove the old crate. That would break anything referencing it. You could yank all the versions of the old crate so no new code depends on it.
David Harvey-Macaulay
@alteous
Aug 30 2017 21:29
@jamessan Thanks. Good to know.