Skip to content

Instantly share code, notes, and snippets.

@kiliankoe
Last active January 18, 2017 15:45
Show Gist options
  • Save kiliankoe/7f4e83eae3c1dce774dc819d15ef867b to your computer and use it in GitHub Desktop.
Save kiliankoe/7f4e83eae3c1dce774dc819d15ef867b to your computer and use it in GitHub Desktop.
Returning results and options in Rust and Swift
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
}
// 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