Book: Reasoning behind naming the 'expect' method

Created on 21 Nov 2016  路  3Comments  路  Source: rust-lang/book

This is more of a general rust learning comment, but also applies to the book here:

https://github.com/rust-lang/book/blob/master/src/ch02-00-guessing-game-tutorial.md

...where error handling is first introduced. In other languages the try/carch/except scheme is easy to make sense of at a quick glance, but I can't attach any logic to the naming of the expect method. Why isn't it onError?

Most helpful comment

I agree, this name is not intuitive. I would have thought something like .catch(), .handle(), .onError(), .fail(), .guard(), .except(), .... the options are endless, .expect() just doesn't make sense

All 3 comments

The way I think about it is that I expect this value to be Some/Ok, and if not, the program should crash because my expectation is incorrect. Does that help?

I tried to research into the origins of the name expect and it's a bit muddled up with naming of other functions and I don't think I've found where expect was created, but this thread (and the linked comment in particular) might interest you: https://internals.rust-lang.org/t/settling-some-key-naming-conventions/269/28

In general, Rust's error handling is definitely not exception-based, and has a different philosophy than you might be used to from other languages. Chapter 9 is devoted entirely to error handling for that reason!

Thank you.

You might consider that that this introductory chapter is the first time many of us coming from exception-based languages are encountering the Rust error handling system. It seems like a small thing, but the somewhat arbitrary feel to this method name is not a helpful first encounter with a language design point I'm already feeling uneasy about. I'll look ahead to Chapter 9 and see if I can make a concrete suggestion regarding this point in Chapter 2.

-Chris

I agree, this name is not intuitive. I would have thought something like .catch(), .handle(), .onError(), .fail(), .guard(), .except(), .... the options are endless, .expect() just doesn't make sense

Was this page helpful?
0 / 5 - 0 ratings