Last active
January 18, 2017 15:45
-
-
Save kiliankoe/7f4e83eae3c1dce774dc819d15ef867b to your computer and use it in GitHub Desktop.
Returning results and options in Rust and Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum TestErr { | |
MuchFatalSuchWow, | |
} | |
fn return_result() -> Result<String, TestErr> { | |
// String::new() // doesn't work | |
// TestErr::MuchFatalSuchWow // doesn't work | |
Err(TestErr::MuchFatalSuchWow) | |
} | |
fn return_option() -> Option<String> { | |
// String::new() // doesn't work | |
None | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I wish Swift had a built-in Result type :/ | |
enum Result<T,E> { | |
case ok(T) | |
case err(E) | |
} | |
enum TestError: Error { // actually being an Error is optional for this use-case | |
case muchFatalSuchWow | |
} | |
func return_result() -> Result<String, TestError> { | |
// return String() // doesn't work | |
// return TestError.muchFatalSuchWow // doesn't work | |
return .err(.muchFatalSuchWow) | |
} | |
func return_optional() -> String? { | |
return "" // this works | |
// .some("") // also works | |
// return .none // also works | |
// return nil // also works | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment