Bleh start again
This commit is contained in:
parent
e34eb0d48f
commit
c6d3ef3505
4
rustbyexample/01/Cargo.lock
generated
4
rustbyexample/01/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "01"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "01"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,26 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
println!("{} days", 31);
|
|
||||||
|
|
||||||
println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");
|
|
||||||
|
|
||||||
println!("{subject} {verb} {object}",
|
|
||||||
object="the lazy dog",
|
|
||||||
subject="the quick brown fox",
|
|
||||||
verb="jumps over");
|
|
||||||
|
|
||||||
println!("{} of {:b} people know binary, the other half don't", 1, 2);
|
|
||||||
|
|
||||||
println!("{number:>width$}", number=1, width=6);
|
|
||||||
|
|
||||||
println!("{number:0>width$}", number=1, width=6);
|
|
||||||
|
|
||||||
println!("My name is {0}, {1} {0}", "Bond", "James");
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
struct Structure(i32);
|
|
||||||
|
|
||||||
// println!("This struct `{}` won't print...", Structure(3));
|
|
||||||
|
|
||||||
let pi = 3.141592;
|
|
||||||
println!("Pi is roughly {:.*}", 3, pi);
|
|
||||||
}
|
|
4
rustbyexample/0122/Cargo.lock
generated
4
rustbyexample/0122/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "0122"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "0122"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,61 +0,0 @@
|
|||||||
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 Point2 {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Point2 {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "x: {}, y: {}", self.x, self.y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Complex {
|
|
||||||
real: f64,
|
|
||||||
imag: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Complex {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "real: {}, imag: {}", 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 range is {small}",
|
|
||||||
small=small_range,
|
|
||||||
big=big_range);
|
|
||||||
|
|
||||||
let point = Point2 { x: 3.3, y: 7.2 };
|
|
||||||
|
|
||||||
println!("Compare points:");
|
|
||||||
println!("Display: {}", point);
|
|
||||||
println!("Debug: {:?}", point);
|
|
||||||
|
|
||||||
let compl = Complex { real: 3.3, imag: 7.2 };
|
|
||||||
|
|
||||||
println!("Compare complex:");
|
|
||||||
println!("Display: {}", compl);
|
|
||||||
println!("Debug: {:?}", compl);
|
|
||||||
}
|
|
4
rustbyexample/01221/Cargo.lock
generated
4
rustbyexample/01221/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "01221"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "01221"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,23 +0,0 @@
|
|||||||
use std::fmt;
|
|
||||||
|
|
||||||
struct List(Vec<i32>);
|
|
||||||
|
|
||||||
impl fmt::Display for List {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
let List(ref vec) = *self;
|
|
||||||
|
|
||||||
try!(write!(f, "["));
|
|
||||||
|
|
||||||
for (count, v) in vec.iter().enumerate() {
|
|
||||||
if count != 0 { try!(write!(f, ", ")); }
|
|
||||||
try!(write!(f, "{}: {}", count, v));
|
|
||||||
}
|
|
||||||
|
|
||||||
write!(f, "]")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let v = List(vec![1, 2, 3]);
|
|
||||||
println!("{}", v);
|
|
||||||
}
|
|
4
rustbyexample/0123/Cargo.lock
generated
4
rustbyexample/0123/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "0123"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "0123"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,50 +0,0 @@
|
|||||||
use std::fmt::{self, Formatter, Display};
|
|
||||||
|
|
||||||
struct City {
|
|
||||||
name: &'static str,
|
|
||||||
lat: f32,
|
|
||||||
lon: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for City {
|
|
||||||
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 { 'S' };
|
|
||||||
|
|
||||||
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 ({}, {}, {}) 0x{:02X}{:02X}{:02X}", self.red, self.green, self.blue, self.red, self.green, self.blue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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() {
|
|
||||||
println!("{:?}", *color);
|
|
||||||
println!("{}", *color);
|
|
||||||
}
|
|
||||||
}
|
|
4
rustbyexample/022/Cargo.lock
generated
4
rustbyexample/022/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "022"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "022"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,54 +0,0 @@
|
|||||||
use std::fmt;
|
|
||||||
|
|
||||||
fn reverse(pair: (i32, bool)) -> (bool, i32) {
|
|
||||||
let (integer, boolean) = pair;
|
|
||||||
(boolean, integer)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn transpose(matrix: Matrix) -> Matrix {
|
|
||||||
Matrix(matrix.0, matrix.2, matrix.1, matrix.3)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Matrix(f32, f32, f32, f32);
|
|
||||||
|
|
||||||
impl fmt::Display for Matrix {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "( {} {} )\n( {} {} )", self.0, self.1, self.2, self.3)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let long_tuple = (1u8, 2u16, 3u32, 4u64,
|
|
||||||
-1i8, -2i16, -3i32, -4i64,
|
|
||||||
0.1f32, 0.2f64,
|
|
||||||
'a', true);
|
|
||||||
|
|
||||||
println!("long tuple first value: {}", long_tuple.0);
|
|
||||||
println!("long tuple second value: {}", long_tuple.1);
|
|
||||||
|
|
||||||
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
|
|
||||||
|
|
||||||
println!("tuple of tuples: {:?}", tuple_of_tuples);
|
|
||||||
|
|
||||||
let pair = (1, true);
|
|
||||||
println!("pair is {:?}", pair);
|
|
||||||
|
|
||||||
println!("the reversed pair is {:?}", reverse(pair));
|
|
||||||
|
|
||||||
println!("one element tuple: {:?}", (5u32,));
|
|
||||||
println!("just an integer: {:?}", (5u32));
|
|
||||||
|
|
||||||
let tuple = (1, "hello", 4.5, true);
|
|
||||||
|
|
||||||
let (a, b, c, d) = tuple;
|
|
||||||
println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
|
|
||||||
|
|
||||||
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
|
|
||||||
println!("{:?}", matrix);
|
|
||||||
|
|
||||||
println!("{}", matrix);
|
|
||||||
|
|
||||||
println!("Matrix:\n{}", matrix);
|
|
||||||
println!("Transpose:\n{}", transpose(matrix));
|
|
||||||
}
|
|
4
rustbyexample/031/Cargo.lock
generated
4
rustbyexample/031/Cargo.lock
generated
@ -1,4 +0,0 @@
|
|||||||
[root]
|
|
||||||
name = "031"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "031"
|
|
||||||
version = "0.1.0"
|
|
||||||
authors = ["Dan Buch <daniel.buch@gmail.com>"]
|
|
||||||
|
|
||||||
[dependencies]
|
|
@ -1,64 +0,0 @@
|
|||||||
struct Nil;
|
|
||||||
|
|
||||||
struct Pair(i32, f32);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Point {
|
|
||||||
x: f32,
|
|
||||||
y: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Rectangle {
|
|
||||||
p1: Point,
|
|
||||||
p2: Point,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rect_area(rect: Rectangle) -> f32 {
|
|
||||||
let Rectangle {
|
|
||||||
p1: Point { x: x1, y: y1 },
|
|
||||||
p2: Point { x: x2, y: y2 }
|
|
||||||
} = rect;
|
|
||||||
((x1 - x2) * (y1 - y2)).abs()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn square(point: Point, dim: f32) -> Rectangle {
|
|
||||||
Rectangle {
|
|
||||||
p1: Point { x: point.x, y: point.y },
|
|
||||||
p2: Point { x: point.x + dim, y: point.y + dim },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let point: Point = Point { x: 0.3, y: 0.4 };
|
|
||||||
|
|
||||||
println!("point coordinates: ({}, {})", point.x, point.y);
|
|
||||||
|
|
||||||
let Point { x: my_x, y: my_y } = point;
|
|
||||||
|
|
||||||
let _rectangle = Rectangle {
|
|
||||||
p1: Point { x: my_y, y: my_x },
|
|
||||||
p2: Point { x: point.x, y: point.y },
|
|
||||||
};
|
|
||||||
|
|
||||||
let _nil = Nil;
|
|
||||||
|
|
||||||
let pair = Pair(1, 0.1);
|
|
||||||
|
|
||||||
println!("pair contains {:?} and {:?}", pair.0, pair.1);
|
|
||||||
|
|
||||||
let Pair(integer, decimal) = pair;
|
|
||||||
|
|
||||||
println!("pair contains {:?} and {:?}", integer, decimal);
|
|
||||||
|
|
||||||
println!("rect is {:?}", _rectangle);
|
|
||||||
println!("rect area is {}", rect_area(_rectangle));
|
|
||||||
|
|
||||||
let dim = 9.1f32;
|
|
||||||
|
|
||||||
println!("point is {:?}", point);
|
|
||||||
println!("dim is {:?}", dim);
|
|
||||||
let sq = square(point, dim);
|
|
||||||
println!("square is {:?}", sq);
|
|
||||||
println!("square area is {}", rect_area(sq));
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user