Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the standard library implement conversions from each type to itself?

On page 298 of Programming Rust you can find the following statement

The standard library automatically implements the trivial conversion from each type to itself: every type T implements From<T> and Into<T>

However it doesn't actually say why that would be useful or why the standard library would automatically do that? What's the reason for this?

like image 786
NO WAR WITH RUSSIA Avatar asked Dec 08 '22 11:12

NO WAR WITH RUSSIA


1 Answers

It can be convenient when writing functions/types with generic arguments. For instance, if I write a function:

fn do_something_with_string<S: Into<String>>(s: S) {}

That function will work equally for String and anything which can be turned into one.

like image 91
Daniel Wagner-Hall Avatar answered Mar 06 '23 06:03

Daniel Wagner-Hall