Last active
October 5, 2015 06:13
-
-
Save zeroows/1ad52e1d7ce556311cf0 to your computer and use it in GitHub Desktop.
Testing Rust with writing to a file and formating functionallity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::fmt; | |
struct Info { | |
name: String, | |
age: i32, | |
rating: i32, | |
} | |
impl fmt::Display for Info { | |
// `f` is a buffer, this method must write the formatted string into it | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
writeln!(f, "name: {}, age: {}, rating: {}", self.name, self.age, self.rating) | |
} | |
} | |
let mut file = File::create("my_best_friends.txt").unwrap(); | |
for info in [ | |
Info { name: String::from("Abdulrhman A"), age: (30), rating: 100 }, | |
Info { name: String::from("Ali S"), age: (39), rating: 85 }, | |
Info { name: String::from("Waleed A"), age: (28), rating: 73 }, | |
].iter() { | |
let _ = writeln!(&mut file, "{}", *info); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment