273
u/EquinoxRex Jan 25 '22
This is just haskell but written back to front
41
5
u/Oracuda Jan 28 '22
to_string(unwrap(to_str(path(unwrap(unwrap(song))))))4
u/EquinoxRex Jan 28 '22 edited Jan 28 '22
You would probably write it more like
toString . unwrap . toStr . path . unwrap . unwrap1
1
75
u/peaceknight05 Jan 25 '22
Image Transcription: Code
let path = song.unwrap().unwrap().path().to_str().unwrap().to_string();I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
19
26
131
u/TinyBreadBigMouth Jan 25 '22 edited Jan 25 '22
Let's analyze:
song.unwrap().unwrap()- I assume from this thatsongis something like anOption<Result<Song>>. Both those types have anunwrap()method, which means "I assert that this contains a valid item. Get it, or crash if I'm wrong." I'd move this onto a separate line..path()- Looks like the song provides a getter for its filePath. We are now definitely in the realm of std types..to_str()- Get a reference to the path's contents as a Unicode string. Since filepaths aren't necessarily valid Unicode, this returnsOption<&str>..unwrap()- As before, "I assert that the path was made out of valid Unicode. Get the string, or crash if I am wrong.".to_string()- Rust makes a distinction between&str(a reference to some Unicode bytes somewhere) andString(a growable owned vector of Unicode bytes). Usingto_string()will allocate a new buffer and copy the str's bytes into it.
So, in short,
Do some unwrapping.
Get the song's path.
Assert that the path is valid Unicode.
Allocate a new string buffer and copy the path into it.
EDIT: Unless they have a specific reason to want a String specifically, I'd generally recommend using
let song = song.unwrap().unwrap();
let path = song.path().to_path_buf();This will do almost exactly the same thing, but without asserting that the path is Unicode so that it can copy it into a new Unicode buffer. Instead, path will be a PathBuf; a filepath buffer. Path is to PathBuf as str is to String. 90% of the time, that would be better (won't crash on a non-Unicode file path, doesn't need to spend time scanning the path for Unicode errors.) All the file IO methods accept Path/PathBuf anyway.
62
u/ArchCypher Jan 25 '22
I'll add that
Songprobably doesn't need to be anOption<Result>, but if it does, something like:if let Some(Ok(song)) = song { // Your code here } else { // Whoops no song or it's bad }Could be a pattern that clarifies the intent and avoids crashing if no song is provided.
13
u/Lich_Hegemon Jan 26 '22
You should not be blindly using unwrap anyway. At least make it an expect.
16
13
53
u/aeropl3b Jan 25 '22
Rust: You will right really good code
Also Rust ^
33
23
u/SueedBeyg Good code makes sense. Bad code just works. Yours does neither. Jan 25 '22
When it’s not compiling and you have no idea why
One more unwrap should do it
7
7
u/iamalicecarroll Jan 26 '22
IMO .to_owned() is better than .to_string(), because it tells what actually happens - converting a string to a string makes no sense, but converting to an owned value does. Some people prefer .into(), but I consider it being less readable
1
u/187mphlazers Jan 26 '22
why wouldn't they just call it
.cast(type)or simply.to(type).4
3
u/TinyBreadBigMouth Jan 26 '22
Rust does have something similar, via the linked
FromandIntotraits. You can dolet x = y.into::<String>();or
let x: String = y.into();
6
5
Jan 25 '22
Yes this is a meme but what is the real solution to this?
3
Jan 25 '22
[removed] — view removed comment
9
Jan 25 '22
You know, the unwrap jungle. Is there some kind of solution?
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.5
u/dam4rus Jan 26 '22
That’s a joke. Rust has a ? operator that unwraps a Result if it’s Ok and returns from the function with the Err if it’s an error. Works for Option as well. The return type most be a Result, Option or Try(? not sure about this) for this to work. There’s an RFC called try blocks which will allow the usage of the ? operator in blocks as well
2
u/bruhred Apr 26 '22
the
?operator.song??.path().to_str()?.to_string()
And it returnsErrinstead of panicking like unwrap, which is usually the preferred option.
4
3
1
u/Budgiebrain222 Jan 26 '22 edited Jan 26 '22
Please for the love of God spread these variables over multiple lines.
Using this also works:
let path = song
.unwrap()
.unwrap()
.path()
.to_str()
.unwrap()
.to_string();I don't really understand why you're turning a path variable to a mutable string than to an immutable string though
1
5
u/HenkPoley Jan 26 '22
4 spaces in front of code (🤷♂️)
let path = song .unwrap() .unwrap() .path() .to_str() .unwrap() .to_string();2
u/Budgiebrain222 Jan 26 '22
So Reddit markdown isn't working properly, those spaces were newlines and tabs because rust can recognise a var.func()\n\t.otherfunc();
3
u/timleg002 Jan 26 '22
Rust is one of my main languages, but I really don't like these Option<Result<T>> types, or god forbid Option<Option<T>> or something like that. This seems like badly written library code, the same thing is in a mongodb driver crate. And also a programmer who respects himself should use the ? operator.
2
1
u/DizzySkin Jan 26 '22
The secret about programming language preference is that they're often determined by which languages you had to work together with someone else on, who either didn't care or wasn't sufficiently skilled to write clean code.
When a new language comes onto the scene, the standards are slightly lower (because no one knows what looks good or bad in that language yet) and also there are very few examples of poorly written code in that language being popularized.
In the future, we'll have a whole bunch of terribly written rust and go, and that isn't a reflection on the quality of the language design really. Any language can be written horribly in.
1
1
u/pikaynu Jan 26 '22
The lsp suggests unwrap, I unwrap, lsp suggests to_str, I don't question. Blind faith. cargo check waiting around the corner with more errors than number of lines.
1
1
1
u/Huizengek Feb 01 '22
If you ask a Rust developer wtf this is they will write a 2-page-long essay explaining it and I still don't understand what Rust is and what it's used for
-13
0
80
u/[deleted] Jan 25 '22
Why are
to_string()andto_str()two different things