About Rust's map function

Asked 2 years ago, Updated 2 years ago, 82 views

I am currently studying Rust, but I am troubled because I do not understand the map very well.

If you look at Rust's explanation, it is interpreted that the closure given when it is called is executed.At the very least, the map is a function implemented in the Iterior structure.Therefore, it is understandable that maps are implemented for things like vector.

However, what I don't understand is that I've seen the code running the map function on the UnboudedReceiver at features::sync::mpsc.
The code looks like the one below.

let(tx,rx)=future::sync::mpsc::unbound();
leta = rx.map (|A|processing).map (|B|processing);

The following are the questions.
1. rx (Unbounded Receiver) should not have implemented Iterator, but why can the map call?
2. When will the closure given to the map be executed?
3. What is the value of A and B?

It's too mysterious to understand.If anyone knows, please reply.

rust

2022-09-30 18:27

1 Answers

Hello.

1.
Rust allows you to implement map even if you are not an iterator because each type of method has a separate namespace.

structure YourType<T>(T);
impl<T>YourType<T>{
  US>fn map (&self) {
    println!("your map");
  }
}

The mpsc UnboundReceiver implements Stream (StreamExt.

2.
This is difficult.Of course, each map has a different timing, so this section describes the behavior of map in StreamExt.

The direct answer is when next of Map, the return value of map, is called.Intuitively, it is "just before extracting the value."

3.
Type Item of StreamExt.For example,
A is the type of value that comes from the channel.Although it does not appear to be the type, the compiler only deduces the type of item at the time of creation of the channel.
B is the resulting type of |A|processing.This can be read from the Stream definition to in Map.

There are a few links to the document, so you may be overwhelmed, but if you have any questions, please ask me again.


2022-09-30 18:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.