]> git.lizzy.rs Git - rust.git/blob - tests/ui/traits/to-str.rs
Rollup merge of #106638 - RalfJung:realstd, r=thomcc
[rust.git] / tests / ui / traits / to-str.rs
1 // run-pass
2 #![allow(non_camel_case_types)]
3
4
5 trait to_str {
6     fn to_string_(&self) -> String;
7 }
8
9 impl to_str for isize {
10     fn to_string_(&self) -> String { self.to_string() }
11 }
12
13 impl<T:to_str> to_str for Vec<T> {
14     fn to_string_(&self) -> String {
15         format!("[{}]",
16                 self.iter()
17                     .map(|e| e.to_string_())
18                     .collect::<Vec<String>>()
19                     .join(", "))
20     }
21 }
22
23 pub fn main() {
24     assert_eq!(1.to_string_(), "1".to_string());
25     assert_eq!((vec![2, 3, 4]).to_string_(), "[2, 3, 4]".to_string());
26
27     fn indirect<T:to_str>(x: T) -> String {
28         format!("{}!", x.to_string_())
29     }
30     assert_eq!(indirect(vec![10, 20]), "[10, 20]!".to_string());
31
32     fn indirect2<T:to_str>(x: T) -> String {
33         indirect(x)
34     }
35     assert_eq!(indirect2(vec![1]), "[1]!".to_string());
36 }