How to call Iter::chain and what will be the return type?

Office Batman

I'm trying to create an iterator that will add one element to the start of a vector

struct World {
    player: Object,
    npcs: Vec<Object>,
}

impl World {
    pub fn all_objects(&mut self) -> ???   
}

I understand I need to use the chain function for this but I'm not sure how, or what will the return type be

Maybe someone can explain to me how to use it?

mcarton

The exact types of iterators can be complex. A fast way to determine them is to try to return the wrong type on purpose, and see what the compiler says:

struct Object;

struct World {
    player: Object,
    npcs: Vec<Object>,
}

impl World {
    pub fn all_objects(&mut self) -> () {
        std::iter::once(&self.player).chain(self.npcs.iter())
    }
}

(Permalink to the playground)

This gives:

error[E0308]: mismatched types
  --> src/lib.rs:10:9
   |
9  |     pub fn all_objects(&mut self) -> () {
   |                                      -- expected `()` because of return type
10 |         std::iter::once(&self.player).chain(self.npcs.iter())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: try adding a semicolon: `;`
   |         |
   |         expected `()`, found struct `std::iter::Chain`
   |
   = note: expected unit type `()`
                 found struct `std::iter::Chain<std::iter::Once<&Object>, std::slice::Iter<'_, Object>>`

and indeed if you replace that, it will compile:

struct Object;

struct World {
    player: Object,
    npcs: Vec<Object>,
}

impl World {
    pub fn all_objects(
        &mut self,
    ) -> std::iter::Chain<std::iter::Once<&Object>, std::slice::Iter<'_, Object>> {
        std::iter::once(&self.player).chain(self.npcs.iter())
    }
}

But the exact type usually doesn't matter with iterators, so you can simply specify that your function returns some kind of iterator:

struct Object;

struct World {
    player: Object,
    npcs: Vec<Object>,
}

impl World {
    pub fn all_objects(&mut self) -> impl Iterator<Item = &Object> {
        std::iter::once(&self.player).chain(self.npcs.iter())
    }
}

(Permalink to the playground)

This is also more flexible, as it will let you change the implementation without affecting the users of the function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What should be the return type of iter().cloned().filter().map()

From Dev

What should be the return type of iter().cloned().filter().map()

From Dev

How to fix the return type of the last promise in a chain?

From Dev

Chain Scala Futures return type

From Dev

Chain optionals with void return type

From Dev

Chain different return in Mockito for retryWhen call

From Dev

Call a chain of functions that return a status and an error message

From Dev

How to call this promise chain right

From Dev

How can I specify a return type for a $http call with Typescript?

From Dev

How do I call map() on an IntStream and return a different type?

From Dev

How to call a method with return type as BOOL after Delay?

From Dev

How do I call map() on an IntStream and return a different type?

From Dev

How to call async method inside a method which has return type?

From Dev

How to call a method with return type as BOOL after Delay?

From Dev

What is the type of `return` or `del`?

From Dev

What is the return type of this pointer?

From Dev

How to chain call functions by using a string containing that chain in PHP

From Dev

Function call with a union return type

From Dev

What is the return value of a recursive call

From Dev

How to make iter method of a class return a value without running the for loop?

From Dev

How to use iter and next in a function to return a list of peaks?

From Dev

What type of Typescript return value should I use for a $http call that returns nothing other than success?

From Dev

How to chain a function call in D3?

From Dev

How to achieve arbitrary chain on function call in javascript?

From Dev

Bash: How to get the call chain on errors?

From Dev

How to detect that the method is in the middle of the call chain in Perl?

From Dev

How to make synchronous chain call with Angular 2?

From Dev

how to chain function return codes in bash script

From Dev

C# What alternative ways are there to propagate an Event down a call chain?

Related Related

  1. 1

    What should be the return type of iter().cloned().filter().map()

  2. 2

    What should be the return type of iter().cloned().filter().map()

  3. 3

    How to fix the return type of the last promise in a chain?

  4. 4

    Chain Scala Futures return type

  5. 5

    Chain optionals with void return type

  6. 6

    Chain different return in Mockito for retryWhen call

  7. 7

    Call a chain of functions that return a status and an error message

  8. 8

    How to call this promise chain right

  9. 9

    How can I specify a return type for a $http call with Typescript?

  10. 10

    How do I call map() on an IntStream and return a different type?

  11. 11

    How to call a method with return type as BOOL after Delay?

  12. 12

    How do I call map() on an IntStream and return a different type?

  13. 13

    How to call async method inside a method which has return type?

  14. 14

    How to call a method with return type as BOOL after Delay?

  15. 15

    What is the type of `return` or `del`?

  16. 16

    What is the return type of this pointer?

  17. 17

    How to chain call functions by using a string containing that chain in PHP

  18. 18

    Function call with a union return type

  19. 19

    What is the return value of a recursive call

  20. 20

    How to make iter method of a class return a value without running the for loop?

  21. 21

    How to use iter and next in a function to return a list of peaks?

  22. 22

    What type of Typescript return value should I use for a $http call that returns nothing other than success?

  23. 23

    How to chain a function call in D3?

  24. 24

    How to achieve arbitrary chain on function call in javascript?

  25. 25

    Bash: How to get the call chain on errors?

  26. 26

    How to detect that the method is in the middle of the call chain in Perl?

  27. 27

    How to make synchronous chain call with Angular 2?

  28. 28

    how to chain function return codes in bash script

  29. 29

    C# What alternative ways are there to propagate an Event down a call chain?

HotTag

Archive