More of the hello of rust by example
This commit is contained in:
parent
bef89a0bee
commit
4fcb2a2e55
4
rustbyexample/.gitignore
vendored
4
rustbyexample/.gitignore
vendored
@ -1,2 +1,2 @@
|
|||||||
/hello/hello
|
/hello/*
|
||||||
/hello/print
|
!/hello/*.rs
|
||||||
|
18
rustbyexample/hello/print_debug.rs
Normal file
18
rustbyexample/hello/print_debug.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
struct Structure(i32);
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Deep(Structure);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("{:?} months in a year.", 12);
|
||||||
|
println!(
|
||||||
|
"{1:?} {0:?} is the {actor:?} name.",
|
||||||
|
"Slater",
|
||||||
|
"Christian",
|
||||||
|
actor = "actor's"
|
||||||
|
);
|
||||||
|
|
||||||
|
println!("Now {:?} will print!", Structure(3));
|
||||||
|
println!("Now {:?} will print!", Deep(Structure(7)));
|
||||||
|
}
|
13
rustbyexample/hello/print_debug2.rs
Normal file
13
rustbyexample/hello/print_debug2.rs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
struct Person<'a> {
|
||||||
|
name: &'a str,
|
||||||
|
age: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let name = "Peter";
|
||||||
|
let age = 27;
|
||||||
|
let peter = Person { name, age };
|
||||||
|
|
||||||
|
println!("{:#?}", peter);
|
||||||
|
}
|
14
rustbyexample/hello/print_display.rs
Normal file
14
rustbyexample/hello/print_display.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
struct Structure(i32);
|
||||||
|
|
||||||
|
impl fmt::Display for Structure {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let s = Structure(4);
|
||||||
|
println!("s={}", s);
|
||||||
|
}
|
45
rustbyexample/hello/print_display2.rs
Normal file
45
rustbyexample/hello/print_display2.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct MinMax(i64, i64);
|
||||||
|
|
||||||
|
impl fmt::Display for MinMax {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "({}, {})", self.0, self.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Point2D {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Point2D {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "x: {}, y: {}", self.x, self.y)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user