Skip to content

Instantly share code, notes, and snippets.

@adamscott
Last active June 15, 2016 22:54
Show Gist options
  • Save adamscott/d3c180bf7a3b20af852a8ccab27549f0 to your computer and use it in GitHub Desktop.
Save adamscott/d3c180bf7a3b20af852a8ccab27549f0 to your computer and use it in GitHub Desktop.
My activities in http://rustbyexample.com/
// Gist repository of my activities.
use std::fmt;
// Tuples can be used as function arguments and as return values
fn reverse(pair: (i32, bool)) -> (bool, i32) {
// `let` can be used to bind the members of a tuple to variables
let (integer, boolean) = pair;
(boolean, integer)
}
fn transpose(matrix: Matrix) -> Matrix {
Matrix(matrix.0,matrix.2,matrix.1,matrix.3)
}
// The following struct is for the activity.
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let result = [(self.0,self.1),(self.2,self.3)]
.iter()
.map(|&t| format!("({} {})",t.0,t.1))
.fold(String::new(), |sum, i| {
sum + &i + "\n"
});
write!(f, "{}", &*result)
}
}
fn main() {
// A tuple with a bunch of different types
let long_tuple = (1u8, 2u16, 3u32, 4u64,
-1i8, -2i16, -3i32, -4i64,
0.1f32, 0.2f64,
'a', true);
// Values can be extracted from the tuple using tuple indexing
println!("long tuple first value: {}", long_tuple.0);
println!("long tuple second value: {}", long_tuple.1);
// Tuples can be tuple members
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
// Tuples are printable
println!("tuple of tuples: {:?}", tuple_of_tuples);
let pair = (1, true);
println!("pair is {:?}", pair);
println!("the reversed pair is {:?}", reverse(pair));
// To create one element tuples, the comma is required to tell them apart
// from a literal surrounded by parentheses
println!("one element tuple: {:?}", (5u32,));
println!("just an integer: {:?}", (5u32));
//tuples can be destructured to create bindings
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);
print!("Matrix: \n{}", matrix);
print!("Transpose: \n{}", transpose(matrix));
}
use std::fmt;
// A unit struct
struct Nil;
// A tuple struct
struct Pair(i32, f64);
// A struct with two fields
struct Point {
x: f64,
y: f64,
}
// Structs can be reused as fields of another struct
#[allow(dead_code)]
struct Rectangle {
p1: Point,
p2: Point,
}
impl fmt::Display for Rectangle {
fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Rectangle ({{{}, {}}}, {{{}, {}}})",
self.p1.x,
self.p1.y,
self.p2.x,
self.p2.y
)
}
}
fn rect_area (rectangle: &Rectangle) -> f64 {
let &Rectangle {
p1: Point {x: x1, y: y1 },
p2: Point {x: x2, y: y2 }
} = rectangle;
(x2 - x1).abs() * (y2 - y1).abs()
}
fn square (origin: &Point, side_length: f32) -> Rectangle {
let &Point {x: x1, y: y1} = origin;
Rectangle {
p1: Point {x: x1, y: y1},
p2: Point {x: x1 + side_length as f64, y: y1 + side_length as f64}
}
}
fn main() {
// Instantiate a `Point`
let point: Point = Point { x: 0.3, y: 0.4 };
// Access the fields of the point
println!("point coordinates: ({}, {})", point.x, point.y);
// Destructure the point using a `let` binding
let Point { x: my_x, y: my_y } = point;
let _rectangle = Rectangle {
// struct instantiation is an expression too
p1: Point { x: my_y, y: my_x },
p2: point,
};
// Instantiate a unit struct
let _nil = Nil;
// Instantiate a tuple struct
let pair = Pair(1, 0.1);
// Destructure a tuple struct
let Pair(integer, decimal) = pair;
println!("pair contains {:?} and {:?}", integer, decimal);
let a_normal_rectangle = Rectangle {
p1: Point {x: 0.0, y: 0.0},
p2: Point {x: 10.0, y: 10.0}
};
println!(
"Area of this rectangle [{}]: {} u².",
a_normal_rectangle,
rect_area(&a_normal_rectangle)
);
let square_origin = Point {x: 13.0, y: 8.0};
println!(
"Square of {{{}, {}}} is {}.",
square_origin.x, square_origin.y,
square(&square_origin, 10.0)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment