Hi Louis,
Thank you for your kind comment.
Let me try to respond to your question step by step :
First, a function like Double.TryParse(string input, out double)
would be strongly discouraged in Rust as the output of the function is provided as a parameter ( out double value
). In Rust, immutability is privileged to avoid state sharing errors and improve global security. By the way rather than write the result in a mutable variable provided in parameters, the equivalent function in Rust will typically return a Result<f64,ParseFloatError>.
which you can store in a new variable.
Second, the bool return of TryParse
function indicate if the parsing was successful or not, but not why in the last case. For this kind of function, we can easily guess why a string is not parsable, but a ParseFloatError
can contain a detailed message, some error code, or any think you want (you are not limited on what an Error
type can contain. For more complex functions (a call to a database, a file opening, …) a boolean is probably not enough, you want to know why the function call has failed (the database server is unreachable ?, you do not have the good authorization ?, the server connexion is full ? the file did not exist ? you do not have the access rights on it ?).
Finally, there is a BIG issue with the public static bool TryParse (string? s, out double result); , C# will not force you to check the boolean before moving on. Then you can use the out
value (with his zero value) by mistake.
Rust will enforce you to check the Result
status, I.E. if it is an Ok(something)
or an Err(something)
. My example is very verbose for demonstration purpose, in fact you can manage errors very efficiently with a simple ?
: take a look on : https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator
Hope this will help, and I hope this will arouse your curiosity for this great language :)