These are chat archives for rust-lang/rust

12th
Jun 2018
trsh
@trsh
Jun 12 2018 07:27
Im making the simplest loop_fn
loop_fn(kaka, |xx| {
          Ok(Loop::Break(xx))
        });
And getting some error cannot infer type forE``
Duno how to solve this. What does E mean anyway, nothing in specs
Help!?
Dmitriy
@dpogretskiy
Jun 12 2018 07:32
you need to return futures::future::ok(Loop::Break(xx))
Michal 'vorner' Vaner
@vorner
Jun 12 2018 07:32
A future is generic over two things ‒ the item it returns and error E. Because your future can never fail, it does not know what error it does not use and you need to tell it explicitly somehow. Either a turbofish to the loop_fn, or somehow restrict the variable you set it to.
Dmitriy
@dpogretskiy
Jun 12 2018 07:38
i guess E in that example is because you are trying to rerurn Result<Loop, ???> out of the closure
trsh
@trsh
Jun 12 2018 07:38
@dpogretskiy not really expected enumstd::result::Result, found enumfutures::future::Loop``
Or if i cut result, it's same err :D
@vorner turbofish ? How can I restrict?
Michal 'vorner' Vaner
@vorner
Jun 12 2018 07:42
For example you can say what kind of Result you return. So, Ok::<_, std::io::Error>(Loop::Break(xx))
That ::<…> stuff is called turbofish
trsh
@trsh
Jun 12 2018 07:42
Sound like some Hero from comics
Michal 'vorner' Vaner
@vorner
Jun 12 2018 07:43
and restrict ‒ for example by passing it to another function that expects the error to be some specific error.
trsh
@trsh
Jun 12 2018 07:44
@vorner bt what stands for _, any or empty value?
Michal 'vorner' Vaner
@vorner
Jun 12 2018 07:45
In case it is in type position, it asks the compiler to figure it out for itself.
If it's in variable name position, then it means you don't want that variable, really, to throw it away.
Basically, _ means „I don't care“.
trsh
@trsh
Jun 12 2018 07:46
:D ok
@vorner tnx , tnx @dpogretskiy
trsh
@trsh
Jun 12 2018 07:56
@vorner maybe u also have an option, what to use in a case, where I have a LOOP for futures, that need to be executed on by one (in set order, every next when prev. is finished). The result should combine in 1 another feature, so I know all is done.
Michal 'vorner' Vaner
@vorner
Jun 12 2018 07:57
Make a stream of the futures and then for_each them or something.
Dmitriy
@dpogretskiy
Jun 12 2018 08:19
struct Never;

fn main() {
    let five = 5u32;

    let looper = loop_fn(five, |num| ok::<Loop<u32, u32>, Never>(Loop::Break(num)));
}
there are some types for you, there is a Never somewhere inside futures crate for that purpose, but i didn't have time to find it
Loop has Break and Continue types, and FuturesResult has Item and Error types
all with funky names
trsh
@trsh
Jun 12 2018 08:21
:D ok,
trsh
@trsh
Jun 12 2018 09:17
@dpogretskiy this Function futures::stream::futures_ordered still doesn't call futures in order on by one, but yields in order.. as for stream, I seem to be out of options
trsh
@trsh
Jun 12 2018 09:25
@vorner stream seems to no care about execution order. It runs all included futures, and picks then first returned and so..
trsh
@trsh
Jun 12 2018 09:31
Maybe im wrong
Michal 'vorner' Vaner
@vorner
Jun 12 2018 09:40
@trsh Huh, what? No, stream itself just yields values, in order. It's like a future, but repeated. And for_each takes each value and does something with it ‒ and that something can be running the value if the value is a future. And it waits for one before going for the next one.
Denis Lisov
@tanriol
Jun 12 2018 09:40
@trsh for_each works with futures in order. The same for most other combinators (exceptions are buffered and buffer_unordered).
trsh
@trsh
Jun 12 2018 09:41
Seems I misunderstood that. Ok, tnx
trsh
@trsh
Jun 12 2018 10:03
@tanriol , @vorner 1 more question. If I do stream.for_each(...).map_err().map(|_|{ ?? }); the results will be?
Vec of stuff I return in for_each cycle?
Denis Lisov
@tanriol
Jun 12 2018 10:05
()
trsh
@trsh
Jun 12 2018 10:07
@tanriol so what's scenario getting / collecting the results?
print only :D ?
Denis Lisov
@tanriol
Jun 12 2018 10:09
trsh
@trsh
Jun 12 2018 10:11
ahh ok tnx..
trsh
@trsh
Jun 12 2018 10:21
@tanriol But then I lose the execution order by for_each.. so basically can't have both?
Denis Lisov
@tanriol
Jun 12 2018 10:22
Do you really? I'd expect the execution to still be in order.
trsh
@trsh
Jun 12 2018 10:25
can't tell not enough info. Not written if futures will be executed in order, and next one only when prev is finished.
Denis Lisov
@tanriol
Jun 12 2018 10:33
Futures are executed in order, one after another, by default. Execution of several futures in parallel is an exception, not the rule.
dovreshef
@dovreshef
Jun 12 2018 16:49
expected struct `std::num::ParseIntError`, found type parameter for let size: u64 = size.as_str().parse()?;
I can't understand the error ...
size is a Regex match
Fredrik Portström
@portstrom
Jun 12 2018 17:37
@dovreshef What's the return type of your function?
dovreshef
@dovreshef
Jun 12 2018 18:35
@portstrom The code as written did not work. But let size = size.as_str().parse::<u64>()?; did. The return type is Result<u64, E> where E: serde::de::Error.
Sylwester Rąpała
@xoac
Jun 12 2018 20:10
@dovreshef This make sens parse function need to know what type u expect https://doc.rust-lang.org/beta/std/string/struct.String.html#method.parse
Because parse is so general, it can cause problems with type inference.
Carl Dong
@dongcarl
Jun 12 2018 20:43
Hi all, what's the naming convention for splitting up an app with name foo into two crates, one for the cli interface (binary), and one as a library?
would it be foo and libfoo?
foo and foo_lib?
foo_cli and foo?
Denis Lisov
@tanriol
Jun 12 2018 20:48
A single crate can have both a library target and a binary target (or even multiple binary targets).
Josh
@joshlemer
Jun 12 2018 23:58
Is it common / idiomatic rust to return Result from a function, to cover cases of invalid arguments? Maybe,

fn takes_square_numbers_only(n: usize) -> Result<String> {
  if is_square(n) {
    Ok("Success!")
  } else {
    Err(Errors::NotSquareNumber)
  }
}
Or is Result more for cases that involve IO?