Skip to content

Instantly share code, notes, and snippets.

@Mec-iS
Last active April 11, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mec-iS/f0fc9065a920042cebfe6b8b239115b0 to your computer and use it in GitHub Desktop.
Save Mec-iS/f0fc9065a920042cebfe6b8b239115b0 to your computer and use it in GitHub Desktop.
[RUST] Functions that are exception to uniformity of return type (forever loops, break, exits ...)
/// Taken from "Programming Rust" by Jim Blandy and Jason Orendorff
// Expressions that don’t finish normally are assigned the special type ! ,
// and they’re exempt from the rules about types having to match.
// You can see ! in the function signature of `std::process::exit()`:
fn exit(code: i32) -> !
// The ! means that exit() never returns. It’s a divergent function.
// You can write divergent functions of your own using the same syntax,
// and this is perfectly natural in some cases:
fn serve_forever(socket: ServerSocket, handler: ServerHandler) -> ! {
socket.listen();
loop {
let s = socket.accept();
handler.handle(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment