Skip to content

Conversation

@dcechano
Copy link
Contributor

This PR changes the recommended way to convert to a boxed error Box<dyn Error> for types that impl Error. It changes the map_err(|e| e.into()) to map_err(From::from) which I believe is much more concise and idiomatic (and satisfying!).

Motivation

Like a lot of people I bashed my head on this problem of getting the compiler to accept the mapped error as a Box<dyn Error> and not a Box<CustomError>. I eventually found e.into() and then I realized that means I can use From::from.

I have done some searching on the StackOverflow and the Rust user forums to see how everyone else is solving this problem and I have yet to find an example of someone recommending From::from fn pointer. Everyone just suggests |e| e.into() even though the former does the same thing more concisely.

I think it is just an oversight and it doesn't occur to the person helping that if e.into works, that means From::from(e) necessarily works as well. So I figured I would share this solution in the official example book if it is indeed an improvement over the current recommended method.

Any way, let know what you guys think!

@rustbot
Copy link
Collaborator

rustbot commented Jan 10, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @marioidival (or someone else) some time within the next two weeks.

@marioidival marioidival merged commit 111cfae into rust-lang:master Nov 27, 2025
1 check passed
jhpratt added a commit to jhpratt/rust that referenced this pull request Nov 29, 2025
Update books

## rust-lang/book

2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4
2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC

- Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570)
- use AND for search terms (rust-lang/book#4573)

## rust-lang/reference

4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8
2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC

- document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063)
- remove unused "link reference definitions" (rust-lang/reference#2092)
- Add review process overview to review-policy.md (rust-lang/reference#2088)
- Remove restriction on dereferencing pointers in const (rust-lang/reference#2090)

## rust-lang/rust-by-example

5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c
2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC

- Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906)
- link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909)
- enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976)
- make search less surprising (rust-lang/rust-by-example#1975)
- Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
rust-timer added a commit to rust-lang/rust that referenced this pull request Nov 29, 2025
Rollup merge of #149424 - rustbot:docs-update, r=ehuss

Update books

## rust-lang/book

2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4
2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC

- Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570)
- use AND for search terms (rust-lang/book#4573)

## rust-lang/reference

4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8
2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC

- document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063)
- remove unused "link reference definitions" (rust-lang/reference#2092)
- Add review process overview to review-policy.md (rust-lang/reference#2088)
- Remove restriction on dereferencing pointers in const (rust-lang/reference#2090)

## rust-lang/rust-by-example

5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c
2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC

- Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906)
- link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909)
- enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976)
- make search less surprising (rust-lang/rust-by-example#1975)
- Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants