Try to search your question here, if you can't find : Ask Any Question Now ?

Is there a way to dereference a slice inside mapping an iterator over a bigger slice?

HomeCategory: stackoverflowIs there a way to dereference a slice inside mapping an iterator over a bigger slice?
Avatarjohn asked 4 months ago

I’m trying to use windows() to iterate over slices of a slice Vec (windows only works on slices), but I need to then take the char slice (&[char]) and use a regular char. The problem is since the char slice points to a Vec slice, dereferencing doesn’t work. How do I solve this?

let a = "12345678910111213141516171819";
let vec1: &str = &a;
println!(
    "{:?}",
    vec1.chars()
        .collect::<Vec<char>>()
        .windows(3)
        .map(|b| b.to_digit(10).product())
);

gives me the error

error[E0599]: no method named `to_digit` found for type `&[char]` in the current scope
 --> src/main.rs:9:24
  |
9 |             .map(|b| b.to_digit(10).product())
  |                        ^^^^^^^^

error: aborting due to previous error

I’m trying to get b to convert to a regular char so to_digit can work with it, and then use product to find the whole windows product. I haven’t experimented with product much, but that is for later. I’ve encountered a problem I don’t know how to fix and it’s more important to me how to convert slices of slices to values, then to know how to fix this line of code specifically.

1 Answers
Best Answer
AvatarAmit answered 4 months ago
Your Answer

1 + 12 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com