Compilation error `no method named `reduce` found for struct `std::vec::IntoIter<_>``

I tried to compile the code but got error as below, did I missing something, I can see all iterator type come with reduce function. Should I need to import something to make it compile?

no method named `reduce` found for struct `std::vec::IntoIter<_>` in the current scope (solution.rs)
   |
13 |         let res = tmp.into_iter().reduce(|total, item| {
   |                                      ^^^^^^ method not found in `std::vec::IntoIter<_>`
pub fn maximum_swap(num: i32) -> i32 {
        let mut num = num;
        let mut tmp = vec!();
        while num != 0 {
            tmp.push(num%10);
            num /= 10;
        }
        if tmp.len() == 0 {return 0}
        let last_item = tmp.len()-1;
        let target = tmp.iter().position(|item| item == tmp.iter().max().unwrap()).unwrap();
        tmp.swap(last_item, target);
        let res = tmp.into_iter().reduce(|total, item| {
            return total * 10 + item; 
        });
        return 0;
    }

This exact code compiles fine in playground. Could you share a little more details?

1 Like

Thanks for quick response. It is part of algorithm snippet in leetcode. I am wondering maybe something missing outside of impl Solution result in compilation failure?

impl Solution {
    pub fn maximum_swap(num: i32) -> i32 {
        let mut num = num;
        let mut tmp = vec!();
        while num != 0 {
            tmp.push(num%10);
            num /= 10;
        }
        if tmp.len() == 0 {return 0}
        let last_item = tmp.len()-1;
        let target = tmp.iter().position(|item| item == tmp.iter().max().unwrap()).unwrap();
        // tmp.swap(last_item, target);
        let res = tmp.into_iter().reduce(|total, item| {
            return total * 10 + item; 
        });
        return 0;
    }
}

It probably is, since this code still compiles fine. Could you try to reduce your real code to something that fails to compile and shows the same error on playground?

3 Likes

Maybe check your rust version with rustc -V because reduce() first got stable in rust 1.51.0

6 Likes

If you are using leetcode for learning Rust, note that, AFAIK, many problems are automatically generated ports from other languages like maybe C++ or Java (I don't really know what's the original) and feature a lot of heavily unidiomatic and unergonomic patterns. It starts with the impl Solution block which is entirely pointless, and reminiscent to how languages like Java always require all code to be wrapped up into classes.

Other problems are overuse of Rc<RefCell<>> as a pointer type, or (this latter one I only found online googling terms like "leetcode rust idiomatic" so it's not a personal experience of mine in the short duration of using the site) using Vecs for fixed-lenght lists or pairs when arrays or tuples would be more appropriate. And that's just the blatantly obvious problems.

Of course you can still do them, just be wary of certain problems (especially ones involving e. g. manually implemented linked lists and the like).

4 Likes

Thanks for replying, since LC is not a good place, I didn't able to find any good platform as day2day tiny tasks for rust. Would you have something in your mind?

At the moment, it’s a good time to do the current advent of code challenges; those are language-independent. As a basic setup you can use: Just write the input into a const INPUT: &str = …, either with include_str from a separate file, or just as a literal e.g. at the bottom of your main source file. Then e.g. use INPUT.trim().lines() to get an iterator over all input lines, and remember str::parse to read a number from string, when you need it.

Other than that, I don’t have much experience with leetcode or any of its alternatives, for that matter. I found a (somewhat old) reddit post that lists some sites; a comment also mentions Rustlings, which should be high-quality. However, I’m not sure if that isn’t too beginner-focused, though maybe some of later exercises might be for you.

1 Like

Some other sites that I have already used a bit, which are language-independent (though possibly a bit mathematical)
CSES - CSES Problem Set - Tasks
Archived Problems - Project Euler

From the reddit post I linked, I took a peek at

which does seem to have some good dedicated Rust contents and is also got mentioned multiple times in that reddit discussion.

Hackerrank has a series where they generalize the Project Euler problems. Definitely mathematical and more competitive programming style than production design, but you might end up with things like a prime factorization library after the 17th variation of such.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.