What? No more posting on the disqus blog?

25 views
Skip to first unread message

Mitch Raful

unread,
Feb 11, 2021, 5:27:14 PM2/11/21
to begin...@googlegroups.com
I answered exercise 5 this way.  Is there a problem doing it this way, other than into_iter() is, I believe more flexible with scope, and is probably overkill in this case.

fn main() {
    let nums: Vec<u32> = (1..11).collect();


    for _ in 1..3 {
        for i in nums.clone().into_iter().map(|i| 2 * i ) {
            println!("{}", i);
        }
    }
}

Thanks,

Mitch

Michael Snoyman

unread,
Feb 11, 2021, 11:38:15 PM2/11/21
to begin...@googlegroups.com
Sorry, exercise 5 of which chapter?
--
You received this message because you are subscribed to the Google Groups "Begin Rust" group.
To unsubscribe from this group and stop receiving emails from it, send an email to begin-rust+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Michael Snoyman

unread,
Feb 14, 2021, 1:12:11 AM2/14/21
to Begin Rust
Mitch is looking at Exercise 5 in Chapter 5 of the Rust Crash Course, available here: https://www.snoyman.com/blog/2018/11/rust-crash-course-05-rule-of-three/

There's one additional problem with this approach to the exercise: it creates a relatively expensive clone of the `Vec<u32>`, which isn't necessary here. Instead, it's possible to implement a solution that involves no cloning. (I'm trying to avoid revealing the solution, but if you're stuck, just ask.)  

Sanjeevi

unread,
Oct 14, 2023, 12:25:37 PM10/14/23
to Begin Rust
This is a simple and no type annotation,cloning is needed,
fn main(){
    (1..11)//.take(2)
    .map(|el|el *2)
    .for_each(|el|println!("{}",el));
}
Reply all
Reply to author
Forward
0 new messages