rust - Misleading error passing OsString to a func<T: ToString>, where does Display trait comes from? -


why function call:

use std::string::tostring; use std::ffi::osstring;  fn len<t: tostring>(v: &t) -> usize {     v.to_string().len() }  fn main() {     let text = osstring::from("hello, world!");     let tlen = len(&text);     println!("len('{:?}') = {}", &text, tlen); } 

raise compilation error:

<anon>:10:16: 10:19 error: trait `core::fmt::display` not implemented type `std::ffi::os_str::osstring` [e0277] <anon>:10     let tlen = len(&text);                          ^~~ <anon>:10:16: 10:19 note: `std::ffi::os_str::osstring` cannot formatted default formatter; try using `:?` instead if using format string <anon>:10     let tlen = len(&text);                          ^~~ error: aborting due previous error playpen: application terminated error code 101 

i know code broken osstring not implement tostring.

the trait tostring implemented types implement display (and, in fact, types):

impl<t: fmt::display + ?sized> tostring t {     ... 

so, when compiler looks implementation of tostring, ends trying 1 display , that's trait search fails osstring (display doesn't have same sort of "blanket impl").


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -