Skip to content

Instantly share code, notes, and snippets.

@zeroows
Last active October 5, 2015 06:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroows/1ad52e1d7ce556311cf0 to your computer and use it in GitHub Desktop.
Save zeroows/1ad52e1d7ce556311cf0 to your computer and use it in GitHub Desktop.
Testing Rust with writing to a file and formating functionallity
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