mirror of
https://bitbucket.org/Mattrixwv/rusttutorials.git
synced 2025-12-07 19:13:57 -05:00
Updated naming again
This commit is contained in:
38
1.HelloWorld/2.FormattedPrint/1Debugging.rs
Normal file
38
1.HelloWorld/2.FormattedPrint/1Debugging.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
// Derive the `fmt::Debug` implementation for `Structure`. `Structure`
|
||||
// is a structure which contains a single `i32`.
|
||||
#[derive(Debug)]
|
||||
struct Structure(i32);
|
||||
|
||||
// Put a `Structure` inside of the structure `Deep`. Make it printable
|
||||
// also.
|
||||
#[derive(Debug)]
|
||||
struct Deep(Structure);
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Person<'a>{
|
||||
name: &'a str,
|
||||
age: u8
|
||||
}
|
||||
|
||||
fn main(){
|
||||
//Printing with `{:?}` is similar to with `{}`.
|
||||
println!("{:?} months in a year.", 12);
|
||||
println!("{1:?} {0:?} is the {actor:?} name.",
|
||||
"Slater",
|
||||
"Christian",
|
||||
actor="actor's");
|
||||
|
||||
//`Structure` is printable!
|
||||
println!("Now {:?} will print!", Structure(3));
|
||||
|
||||
//The problem with `derive` is there is no control over how
|
||||
//the results look. What if I want this to just show a `7`?
|
||||
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||
|
||||
let name = "Peter";
|
||||
let age = 27;
|
||||
let peter = Person { name, age };
|
||||
|
||||
//Pretty print
|
||||
println!("{:#?}", peter);
|
||||
}
|
||||
33
1.HelloWorld/2.FormattedPrint/2-1-Testcase-List.rs
Normal file
33
1.HelloWorld/2.FormattedPrint/2-1-Testcase-List.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
//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);
|
||||
}
|
||||
71
1.HelloWorld/2.FormattedPrint/2Display.rs
Normal file
71
1.HelloWorld/2.FormattedPrint/2Display.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use std::fmt; //Import `fmt`
|
||||
|
||||
//A structure holding two numbers. `Debug` will be derived so the results can
|
||||
//be contrasted with `Display`.
|
||||
#[derive(Debug)]
|
||||
struct MinMax(i64, i64);
|
||||
//Implement `Display` for `MinMax`.
|
||||
impl fmt::Display for MinMax{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
|
||||
//Use `self.number` to refer to each positional data point.
|
||||
write!(f, "({}, {})", self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
//Define a structure where the fields are nameable for comparison.
|
||||
#[derive(Debug)]
|
||||
struct Point2D{
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
//Similarly, implement `Display` for `Point2D`
|
||||
impl fmt::Display for Point2D{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
|
||||
//Customize so only `x` and `y` are denoted.
|
||||
write!(f, "x: {}, y: {}", self.x, self.y)
|
||||
}
|
||||
}
|
||||
|
||||
//Define a structure for complex numbers
|
||||
#[derive(Debug)]
|
||||
struct Complex{
|
||||
real: f64,
|
||||
imag: f64,
|
||||
}
|
||||
//Implement 'Display' for Complex
|
||||
impl fmt::Display for Complex{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
|
||||
write!(f, "{} + {}i", self.real, self.imag)
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let minmax = MinMax(0, 14);
|
||||
|
||||
println!("Compare structures:");
|
||||
println!("Display: {}", minmax);
|
||||
println!("Debug: {:?}", minmax);
|
||||
|
||||
let big_range = MinMax(-300, 300);
|
||||
let small_range = MinMax(-3, 3);
|
||||
|
||||
println!("The big range is {big} and the small is {small}",
|
||||
small = small_range,
|
||||
big = big_range);
|
||||
|
||||
let point = Point2D { x: 3.3, y: 7.2 };
|
||||
|
||||
println!("Compare points:");
|
||||
println!("Display: {}", point);
|
||||
println!("Debug: {:?}", point);
|
||||
|
||||
//Error. Both `Debug` and `Display` were implemented, but `{:b}`
|
||||
//requires `fmt::Binary` to be implemented. This will not work.
|
||||
//println!("What does Point2D look like in binary: {:b}?", point);
|
||||
|
||||
//Print a test for Complex
|
||||
let comp = Complex{real: 3.3, imag: 7.2};
|
||||
println!("Compare Complex:");
|
||||
println!("Display: {}", comp);
|
||||
println!("debug: {:?}", comp);
|
||||
}
|
||||
52
1.HelloWorld/2.FormattedPrint/3Formatting.rs
Normal file
52
1.HelloWorld/2.FormattedPrint/3Formatting.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::fmt::{self, Formatter, Display};
|
||||
|
||||
struct City{
|
||||
name: &'static str,
|
||||
//Latitude
|
||||
lat: f32,
|
||||
//Longitude
|
||||
lon: f32,
|
||||
}
|
||||
impl Display for City{
|
||||
//`f` is a buffer, and this method must write the formatted string into it
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result{
|
||||
let lat_c = if self.lat >= 0.0 { 'N' } else { 'S' };
|
||||
let lon_c = if self.lon >= 0.0 { 'E' } else { 'W' };
|
||||
|
||||
//`write!` is like `format!`, but it will write the formatted string
|
||||
//into a buffer (the first argument)
|
||||
write!(f, "{}: {:.3}°{} {:.3}°{}", self.name, self.lat.abs(), lat_c, self.lon.abs(), lon_c)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Color{
|
||||
red: u8,
|
||||
green: u8,
|
||||
blue: u8,
|
||||
}
|
||||
impl Display for Color{
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result{
|
||||
write!(f, "RGB ({0}, {1}, {2}) 0x{0:0width$X}{1:0width$X}{2:0width$X}", self.red, self.green, self.blue, width = 2)
|
||||
}
|
||||
}
|
||||
|
||||
fn main(){
|
||||
for city in[
|
||||
City{ name: "Dublin", lat: 53.347778, lon: -6.259722 },
|
||||
City{ name: "Oslo", lat: 59.95, lon: 10.75 },
|
||||
City{ name: "Vancouver", lat: 49.25, lon: -123.1 },
|
||||
].iter(){
|
||||
println!("{}", *city);
|
||||
}
|
||||
for color in[
|
||||
Color { red: 128, green: 255, blue: 90 },
|
||||
Color { red: 0, green: 3, blue: 254 },
|
||||
Color { red: 0, green: 0, blue: 0 },
|
||||
].iter(){
|
||||
//Switch this to use {} once you've added an implementation
|
||||
//for fmt::Display.
|
||||
//println!("{:?}", *color);
|
||||
println!("{}", *color);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user