Proper way to handle usize/u64 conversion when manipulating and writing bytes?
⚓ Rust 📅 2026-03-05 👤 surdeus 👁️ 2Hey all,
Here are the functions in question: Stuck between usize and u64. - Pastebin.com
What doing is taking a whole file, splitting it into parts, then writing those individual parts to disk. I eventually want to then take those smaller parts and split them further into even smaller parts. I'm running into an issue when I get to the second loop. So at this point, the whole file has been split in half and I'm trying to take one of the halves and split that in half so it's 1/4.
I'm running into an error in line 21 of the pastebin link where it states I can't convert a usize to u64. The usize value is on line 20 with the new_end_byte variable.
If I try to convert the current u64 variables to usize, then I get other errors. Namely, when I then try to write the file slice I get an error on line 31 in the pastebin because the SeekFrom::Start(start) requires a usize.
The compiler says that I can try to convert the usize into u64 with a try_into().unwrap() and that does work but I was hoping there was a more elegant way to do it that would allow for the conversion without relying on an unwrap, expect, etc.
Ultimately I'm just trying to split a file into smaller parts, loop, and split again. If anyone has a different idea of how to do that, I'd be interested to hear it.
Thanks!
5 posts - 4 participants
🏷️ Rust_feed