These are chat archives for rust-lang/rust

13th
Jan 2017
Chris Natoli
@NatoliChris
Jan 13 2017 06:29

Hi all,

I’m new to Rust so this problem is probably super basic, but what I’ve read hasn’t seemed to work for me. :(

I have a function that returns Result<T, DecoderError>, however when trying to assign that Result to a variable, I get the following error: cannot infer type for ‘_’
I’m confused as to why, because in another part of the code [same rust file] the exact line works. I’ve read and tried to match / define the collect type but I couldn’t get it working.

Example line in question:

let foo = my_vec.iter().filter_map(|bytes| SomeStruct::some_func(bytes).ok()).collect();
//  ^^^ cannot infer type for ‘_'

Can anyone point me in the right direction/documentation to read further?

Thank you so much in advance!

Sergey Noskov
@Albibek
Jan 13 2017 06:41
@NatoliChris , I think you need the Generics chapter from The Book. Iterator's collect can collect your data to different type, and compiler cannot know what exact type you want to collect it to. Your example can be solved with .collect::<Vec<_>>(); if you want to collect the results into Vec. The _ will already inferred correctly by compiler.
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 06:43
@NatoliChris might it be that the place where it's inferring correctly has more context given by subsequent use of foo? Ie. Returning it from a function with an explicit return type.
Chris Natoli
@NatoliChris
Jan 13 2017 06:46

@Albibek I’ll read it, thanks!

@rhys-vdw from what I see it has the exact same context, it’s quite a large codebase so I probably overlooked something really small

Thanks!

Rhys van der Waerden
@rhys-vdw
Jan 13 2017 07:49
Hey everyone, does it seem like I'm doing something wrong if I end up with a parameter of type &Rc<SomeStruct>?
Or is it okay to pass an Rc by reference.
Michal 'vorner' Vaner
@vorner
Jan 13 2017 07:52
It's OK. It's probably cheaper than cloning it every time.
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 07:52
Actually I just wonder about this approach in general.
I've got a struct for a generic A* node:
struct Node<State> {
    g_score: u32,
    h_score: u32,
    state: State,
    parent: Option<Rc<Node<State>>>,
}
Aleksey Kladov
@matklad
Jan 13 2017 07:53
Though &SomeStruct is better, if it is sufficient. You can get &T from Rc<T>.
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 07:53
Then I have a trait like:
pub trait Space {
    type State : Clone;
    fn start(&self) -> Self::State;
    fn is_goal(&self, state: &Self::State) -> bool;
    fn expand(&self, state: &Self::State) -> Vec<Self::State>;
    fn distance(&self, from: &Self::State, to: &Self::State) -> u32;
    fn heuristic(&self, from: &Self::State) -> u32;
}
And in my implementation I set State to Rc<Coord>
So then distance impl get the signature:
    fn distance(&self, from: &Rc<Coord>, to: &Rc<Coord>) -> u32 {
distance is called from a context that is generic on State also, so it can't "know" to dereference the Rc<Coord>, and it ends up being passed like so.
If that makes sense...
Aleksey Kladov
@matklad
Jan 13 2017 07:56
Do you have any foo.clone() or to.clone() inside the distance?
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 07:57
    fn distance(&self, from: &Rc<Coord>, to: &Rc<Coord>) -> u32 {
        Coord::distance(&from, &to)
    }
impl Coord {
    pub fn distance(from: &Coord, to: &Coord) -> u32 {
        ((to.y - from.y).abs() + (to.y - from.y).abs()) as u32
    }
Aleksey Kladov
@matklad
Jan 13 2017 07:58
Then fn distance(&self, from: &Coord, to: &Coord) should work I think.
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 07:58
Except then I get compile errors
Because I want the Space trait (above) to be able to handle structs by reference also.
(if that makes sense)
I can't do this:
impl Space for Grid {
    type State = Rc<Coord>;
    // ...
    fn distance(&self, from: &Coord, to: &Coord) -> u32 {
        Coord::distance(&from, &to)
    }
23 |     fn distance(&self, from: &Coord, to: &Coord) -> u32 {
   |                              ^^^^^^ expected struct `std::rc::Rc`, found struct `coord::Coord`
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 08:04
Heh, anyway, sorry if that's confusing. Just trying to get a feel for how I should structure projects.
Aleksey Kladov
@matklad
Jan 13 2017 08:06
Ah yes indeed you are right! I think your solution is correct then. Perhaps you can make just &Coord work if you make two parameterters, StatWrapper and State, with bounds like StateWrapper: Clone + Borrow<State>, but this would imo only complicate things...
Mughal Ahmed
@greenmughal
Jan 13 2017 09:18
Just for self promotion of rust project
https://github.com/greenmughal/rustyplanet
Matanel Levi
@matanelevi
Jan 13 2017 17:40
Hey, how do you suggest to continue with Rust? I've read the book and wrote some code. Still, I can't say that I'm good with Rust. I can think of contributing to a github project - do you familiar with such one for a "Rust newbie"?
Aleksey Kladov
@matklad
Jan 13 2017 18:05
@matanelevi One option is to implement some moderately large project from scratch. A perfect example would be a small ray tracer: https://en.wikipedia.org/wiki/Ray_tracing_(graphics) http://in1weekend.blogspot.ru/2016/01/ray-tracing-in-one-weekend.html. It's also fun to write a tiny programming language.
Michal 'vorner' Vaner
@vorner
Jan 13 2017 18:05
I think some projects (at least the basic repositories) have a label for newbie tasks. I think they call it E-learn or something. But it's just a vague memory of seeing it somewhere.
Or, try to use it and see what you like and contribute to that. Or something that you mostly like, but has some missing bits ‒ that way you may get mentoring (from the leaders of the project/crate/whatever) and also you have better idea what and why you're doing it.
Aleksey Kladov
@matklad
Jan 13 2017 18:07
And yes, @vorner , quite a few Rust projects have specially designated issue. The most famous one is Servo I think? There's also call for participaion this week in rust: https://this-week-in-rust.org/blog/2017/01/10/this-week-in-rust-164/
And there are some nice bugs in Cargo: https://github.com/rust-lang/cargo/labels/E-easy
vorner @vorner knew it started with E-something
Rhys van der Waerden
@rhys-vdw
Jan 13 2017 18:47
Thanks for the feedback @matklad
Matanel Levi
@matanelevi
Jan 13 2017 19:20
Thanks @matklad @vorner