mirror of
https://bitbucket.org/Mattrixwv/rusttutorials.git
synced 2025-12-07 11:03:58 -05:00
34 lines
790 B
Rust
34 lines
790 B
Rust
//Import the `fmt` module.
|
|
use std::fmt;
|
|
|
|
//Define a structure named `List` containing a `Vec`.
|
|
struct List(Vec<i32>);
|
|
|
|
impl fmt::Display for List{
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
|
|
//Extract the value using tuple indexing,
|
|
//and create a reference to `vec`.
|
|
let vec = &self.0;
|
|
|
|
write!(f, "[")?;
|
|
|
|
//Iterate over `v` in `vec` while enumerating the iteration count in `count`.
|
|
for(count, v) in vec.iter().enumerate(){
|
|
//For every element except the first, add a comma.
|
|
//Use the ? operator, or try!, to return on errors.
|
|
if count != 0{
|
|
write!(f, ", ")?;
|
|
}
|
|
write!(f, "{}: {}", count, v)?;
|
|
}
|
|
|
|
//Close the opened bracket and return a fmt::Result value.
|
|
write!(f, "]")
|
|
}
|
|
}
|
|
|
|
fn main(){
|
|
let v = List(vec![1, 2, 3]);
|
|
println!("{}", v);
|
|
}
|