RBE's hello/print/print_display/testcase_list
This commit is contained in:
parent
5568eb4409
commit
49889a15b8
23
rustbyexample/hello/testcase_list.rs
Normal file
23
rustbyexample/hello/testcase_list.rs
Normal file
@ -0,0 +1,23 @@
|
||||
use std::fmt;
|
||||
|
||||
struct List(Vec<i32>);
|
||||
|
||||
impl fmt::Display for List {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let vec = &self.0;
|
||||
|
||||
write!(f, "[")?;
|
||||
|
||||
for (count, v ) in vec.iter().enumerate() {
|
||||
if count != 0 { write!(f, ", ")?; }
|
||||
write!(f, "{}", v)?;
|
||||
}
|
||||
|
||||
write!(f, "]")
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let v = List(vec![1, 2, 3]);
|
||||
println!("{}", v);
|
||||
}
|
Loading…
Reference in New Issue
Block a user