Newbie here. Why would a function ever need to return Result<ResultMessage, ResultMessage>? What could ResultMessage store such that both errors and valid returns could be represented by it?
That's not true. () Is implicitly returned when there is no return, which is perfectly usable for functions that have no return signature.
Also you don't need to return the Result, rather you would return the value itself. This is because both Ok and Err return the same value type. There is no need to wrap them in a Result at that point
A binary search either finds an element and returns its index, or doesn't find it but still identifies the index where it should be. These naturally fit the notions of success and error. The procedure to compute that index is the same, but we need that extra bit to identify found/not found, and it makes sense that you should only be able to use the index after checking what it represents.
You need to know that in general Rust doesnt throw exceptions but gives you a generic Enum of the Type Result instead and then you can do error handling on that. So if you have a structure that would return a ResultMessage-object but it might fail, you get this response type.
Let me clarify. I understand how error handling works in Rust. My problem is that in said Result<T, E>: T==E. I can't think of something that can both represent errors and "ok-branch" values. As this and this comment pointed out, it could basically be a Result<String, String>; but as the second comment noted, that is not how you should do error handling in Rust. As far as I know, you should do something like enum ErrorKind { /* errors */ }.
If the function isn't a public library function, and is only used in the same way as shown in the post (API endpoint?), then I think something like Result<String, String> is fine, if you actually utilize the Result and instead of returning HTTP OK all the time, return a 4xx or 5xx code if an error happened
Thanks for the clarification, I don't understand because Im not familiar with rust and how to develop in it yet. I'm glad others picked up your point and I hope you find an answer to your question
Aside from, y'know, putting what appears to be an error message in what appears to be an HTTP 200 OK response (do not), idk, I think this looks mostly fine
I think it may be okay. It might not be a critical error, and the response might have the appropriate message already. Sure, they could have done it in one swoop, but there’s nothing horrible here.
I guess I was assuming Rust would be way more "low level" feeling - like, C or something. I'm just surprised to see "high level" stuff like json and HttpResponse in a language that's touted as being so close to the hardware. I dunno, I'm a fucking idiot, so this all just blows my mind.
Rust lets you work very close to the metal if you need to, and its memory model forces you to think in a lower-level way than pretty much any other language than C/C++, but it has a lot of fantastic modern bells and whistles.
Also the json/http stuff is from a library, not included with the language by default
Lots of "low-level" system tools speak json over http. Consider that things like nginx, redis, apache are all things would expect to be written in a low-level systems language.
One of the cool things about rust's serializer library (handling e.g. json) is that it usually doesn't need to create additional copies of string data, it can use references without fear of multiple owners modifying the data, due to the borrow checker. It is very fast.
Rust associates low level thinking with high level ergonomics in an absolutely amazing way. Libraries (like those handling http and json) are really easy to install and offer great usability thanks to Rust macros. I can't go back to C++ for the life of me.
Conceptually it's not so bad, but it feels like reading a for loop for the first time. This kind of syntax is actually pretty intuitive once you start to learn Rust.
Honestly, not much of a webdev, and I am in the process of learning rust. But wouldn't HttpResponse::Ok method basically translate to 200 status code in the client side, which would mean that no error actually occured during the operation. Wouldn't handling the error and translating it to the proper status code ( maybe 4xx or 5xx ) be more appropriate?
41
u/Error-42 Feb 04 '23
Newbie here. Why would a function ever need to return
Result<ResultMessage, ResultMessage>? What couldResultMessagestore such that both errors and valid returns could be represented by it?11
u/Fendse Regular people speak regular languages Feb 05 '23
Aside from, y'know, putting what appears to be an error message in what appears to be an HTTP 200 OK response (do not), idk, I think this looks mostly fine
7
u/caliguian Feb 05 '23
I think it may be okay. It might not be a critical error, and the response might have the appropriate message already. Sure, they could have done it in one swoop, but there’s nothing horrible here.
25
u/cheezballs Feb 05 '23
I don't even know how to read this. This is my first exposure to rust and I think I hate it.
10
u/sadboi24-7 Feb 05 '23
JS dev using rust
2
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 05 '23
Image Transcription: Code
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!
3
u/[deleted] Feb 05 '23
Completely reasonable. They are just passing the result, whether it be ok or error, on to the client (HttpResponse). Not bad imo.
1
u/Nall-ohki Feb 05 '23
Not bad code.
1
u/envis10n Feb 05 '23
Typically if you are returning a Result, you are doing so because whatever the operation is could fail and you need to handle the error.
If your Result type uses the same type for both Ok and Err, then you don't need to use Result. Just return whatever the result is.
It makes sense to match inside the function, since the call itself returns a Result. But you don't need to then also return a Result type.
The only situation it would make sense is if you use the
?operator to automatically Err, since that would require a Result type.1
u/Arthex56 Feb 05 '23
Honestly, not much of a webdev, and I am in the process of learning rust. But wouldn't
HttpResponse::Okmethod basically translate to 200 status code in the client side, which would mean that no error actually occured during the operation. Wouldn't handling the error and translating it to the proper status code ( maybe 4xx or 5xx ) be more appropriate?0
u/Public-Bluebird-8318 Feb 05 '23
Hii
0
u/[deleted] Feb 05 '23
[deleted] — view deleted comment