Rust编程语言打字04_Testcase: List
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 {
// Dereference `self` and create a reference to `vec`
// via destructuring.
let List(ref vec) = *self;
try!(write!(f, "["));
// Iterate over `vec` in `v` while enumerating the iteration
// count in `count`.
for (count, v) in vec.iter().enumerate() {
// For every element except the first, add a comma
// before calling `write!`. Use `try!` to return on errors.
if count != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", v));
}
// Close the opened bracket and return a fmt::Result value
write!(f, "]")
}
}
fn main() {
let v = List(vec![1, 2, 3]);
println!("{}", v);
}
// 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 {
// Dereference `self` and create a reference to `vec`
// via destructuring.
let List(ref vec) = *self;
try!(write!(f, "["));
// Iterate over `vec` in `v` while enumerating the iteration
// count in `count`.
for (count, v) in vec.iter().enumerate() {
// For every element except the first, add a comma
// before calling `write!`. Use `try!` to return on errors.
if count != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", v));
}
// Close the opened bracket and return a fmt::Result value
write!(f, "]")
}
}
fn main() {
let v = List(vec![1, 2, 3]);
println!("{}", v);
}
上一篇:Rust编程语言打字06_2.1
声明:以上文章均为用户自行添加,仅供打字交流使用,不代表本站观点,本站不承担任何法律责任,特此声明!如果有侵犯到您的权利,请及时联系我们删除。
文章热度:☆☆☆☆☆
文章难度:☆☆☆☆☆
文章质量:☆☆☆☆☆
说明:系统根据文章的热度、难度、质量自动认证,已认证的文章将参与打字排名!
本文打字排名TOP20
登录后可见
用户更多文章推荐
- Rust编程语言打字11_3.2.12017-01-15
- Rust编程语言打字06_2.12017-01-14
- Rust编程语言打字02_格式化输出_Debu...2017-01-14
- Rust编程语言打字03_格式化输出_Disp...2017-01-14
- Rust编程语言打字01_格式化输出2017-01-13