Discussion on the Naming of expect in Rust

65 points korenhe

Hello, Rust community!

As a beginner, I’ve found the expect function a bit confusing. When I see file.open().expect("file not found"), it seems like "file not found" is an expected outcome, rather than indicating that the function should succeed.

I wonder if a name like expect_or might be clearer, as it would better convey that the function returns a value or triggers the error message. For example:

File::open("xxx").expect_or("file not found");

What do you think? Is this a common concern, or do you feel the current naming is effective? I’d love to hear your thoughts!