Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Stream::copied #442

Merged
1 commit merged into from
Nov 2, 2019
Merged

add Stream::copied #442

1 commit merged into from
Nov 2, 2019

Conversation

recursion128
Copy link
Contributor

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you so much :)

@ghost ghost merged commit 5fb9d3e into async-rs:master Nov 2, 2019
@k-nasa
Copy link
Member

k-nasa commented Nov 2, 2019

This code con't run.

    async_std::task::block_on(async {
        use async_std::prelude::*;
        use std::collections::VecDeque;

        let v: VecDeque<_> = vec![&1, &2, &3].into_iter().collect();

        let mut v_copied  = v.copied();

        dbg!(v);
        dbg!(v_copied);
    });

I got this error message.

src/main.rs:11:14
   |
7  |         let v: VecDeque<_> = vec![&1, &2, &3].into_iter().collect();
   |             - move occurs because `v` has type `std::collections::VecDeque<&i32>`, which does not implement the `Copy` trait
8  |
9  |         let mut v_copied  = v.copied();
   |                             - value moved here
10 |
11 |         dbg!(v);
   |              ^ value used here after move

Is the copied method meaningful?

@ghost
Copy link

ghost commented Nov 2, 2019

Oh no, I think I didn't look closely enough, my bad! :( I'll try to fix in a follow-up PR

@recursion128
Copy link
Contributor Author

recursion128 commented Nov 2, 2019

copied is the same as .map(|&x| x), consume an old stream and create a new stream that copy each element from &T to T.

This code:

use async_std::prelude::*;
use async_std::stream;
use std::collections::VecDeque;


fn main() {
    task::block_on(async {
        let v : VecDeque<_> = vec![1, 2, 3].into_iter().collect();

        let mut s = stream::from_iter(v.iter());

        let mut v_copied = s.copied();

        dbg!(v);

    });
}

output:

[examples/hello-world.rs:15] v = [
    1,
    2,
    3,
  

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants