mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 18:12:43 +00:00
40 lines
974 B
Rust
40 lines
974 B
Rust
// ./src/hello/print/print_display/testcase_list.md
|
|
|
|
|
|
use std::fmt; // Import the `fmt` module.
|
|
|
|
// 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 to return on errors.
|
|
if count != 0 { write!(f, ", ")?; }
|
|
write!(f, "{}", v)?;
|
|
}
|
|
|
|
// Close the opened bracket and return a fmt::Result value.
|
|
write!(f, "]")
|
|
}
|
|
}
|
|
|
|
fn part0() {
|
|
let v = List(vec![1, 2, 3]);
|
|
println!("{}", v);
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|