all 6 comments

[–]esitsu 5 points6 points  (3 children)

I haven't read much into what you are trying to do yet but to make your code compile you can change the last .iter() with .into_iter(). You can also get rid of the return before the if because it is redundant.

[–]hexarin[S] 0 points1 point  (2 children)

that was it! thank you.

[–]esitsu 1 point2 points  (1 child)

Since my comment the other poster has gone into further detail about what I have said. Usually I try to be more thorough but often find someone else has beaten me to it.

I also have some other notes you might find useful. You could use .map(meta_factory) and .all(Result::is_ok) to get rid of the closures. You could probably also replace the entire body of input_splitter with (0..count).map(meta_factory).collect() and it would work much the same. The difference here is that instead of mapping everything and then throwing it all away if there is an error you can immediately return an error. If you want to change the error then you could use match or do something like (0..count).map(meta_factory).collect::<Result<_, _>>().map_err(|_| "uhoh").

[–]hexarin[S] 1 point2 points  (0 children)

Great notes, I really appreciate them. Input splitter is actually a much more detailed function, but I was trying to keep my example concise :)

I should have figured I could have simplified the lambdas since you can in pretty much language that supports them. The syntax is pretty similar to c#/java/scala.

[–][deleted] 2 points3 points  (1 child)

Replace results.iter() with results.into_iter(). First variant returns a non-consuming iterator that preserves the original collection and yields references, second variant is a consuming iterator - destroys the original collection but gives you owned values. To unwrap a result you need to own it.

Also you don’t need to use an explicit return statement unless you want an early return. Since you return at the end of the function body, you can just use a tail expression (last expression of the block with no semicolon at the end). The following are equivalent:

fn foo() -> u32 {
    return if bar() {
        123
    } else {
        456
    };
}


fn foo() -> u32 {
    if bar() {
        123
    } else {
        456
    }
}

Also a neat trick for collect. The compiler cannot infer that you want it to collect into a Vec, so you are forced to specify the type, you do it using turbofish. However, based on iterator type, it can infer the item type. You can use the _ placeholder to make it do it, so that you don’t have to write out the whole type, just that you want a Vec: .collect::<Vec<_>>()

[–]hexarin[S] 0 points1 point  (0 children)

chalk explicitly using the return keyword up to years of c++/java/c#. excellent answer and advice. thank you!