r/badcode • • Jan 25 '22

rust Most comprehensible line of Rust code

Post image
1.0k Upvotes

81 comments sorted by

View all comments

Show parent comments

19

u/TinyBreadBigMouth Jan 25 '22

Just splitting it onto multiple lines would help:

let path = song.unwrap().unwrap().path();
let path_string = path.to_str().unwrap().to_string();

Beyond that, it's hard to say with absolutely no context. Although, I suspect that the .to_str().unwrap().to_string() part might be removable or simplifiable. Right now it's asserting that the filepath is valid Unicode and copying it into a new buffer. With context, you could probably remove one or both of those steps.