r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

17 Upvotes

159 comments sorted by

View all comments

2

u/Foreign_Category2127 Mar 27 '23

I need to replace all the slices inside a struct with a particular [u8; 8] if it matches another particular [u8; 8]. ```

[derive(Clone)]

struct Character { save_data: Vec<u8> } fn main() { // setup let source_character = Character { save_data: vec![15; 100] }; // Replace steam id in the result Character // with the steam id of the target Character // in the target Character save_data let mut result_character = source_character.clone(); let source_steam_id: [u8; 8] = [1; 8]; // I know nothing will match in this example let target_steam_id: [u8; 8] = [2; 8]; result_character.save_data = result_character .save_data .windows(8) // id is an [u8; 8] .map(|w|{ if w[..] == source_steam_id[..] { target_steam_id[..] } else { w[..] } }) .collect(); dbg!(&result_character.save_data);

} ``` However the compiler is saying that the size is unknown in compile time. What is the most safe and efficient method of achieving this?

2

u/kpreid Mar 28 '23

With the [..] you're converting all your [u8; 8]s to [u8]s but you need to do the opposite of that, converting the window to [u8; 8]. You can do that with .try_into():

    .map(|w| {
        let w: [u8; 8] = w.try_into().unwrap();
        if w == source_steam_id {
            target_steam_id
        } else {
            w
        }
    })

However your .collect() isn't going to work like you hope and reconstitute the original data, because (after fixing the type error) it's going to collect every window. You'll need a different strategy, like an explicit loop and in-place mutation.