Put some stuff into lib.rs
This commit is contained in:
parent
45cad795a7
commit
09ff36dff8
124
src/lib.rs
Normal file
124
src/lib.rs
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
use std::ops::{Add, Div, Mul, Sub};
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct Vector3 {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
z: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vector3 {
|
||||||
|
pub fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
||||||
|
Vector3 { x, y, z }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add for Vector3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn add(self, rhs: Vector3) -> Self::Output {
|
||||||
|
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for Vector3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Vector3) -> Self::Output {
|
||||||
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<f64> for Vector3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn mul(self, rhs: f64) -> Self::Output {
|
||||||
|
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul<Vector3> for f64 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||||
|
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mul for Vector3 {
|
||||||
|
type Output = f64;
|
||||||
|
|
||||||
|
fn mul(self, rhs: Vector3) -> Self::Output {
|
||||||
|
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Div<f64> for Vector3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn div(self, rhs: f64) -> Self::Output {
|
||||||
|
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone)]
|
||||||
|
pub struct Point3 {
|
||||||
|
x: f64,
|
||||||
|
y: f64,
|
||||||
|
z: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Point3 {
|
||||||
|
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
|
||||||
|
Point3 { x, y, z }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Add<Vector3> for Point3 {
|
||||||
|
type Output = Point3;
|
||||||
|
|
||||||
|
fn add(self, rhs: Vector3) -> Self::Output {
|
||||||
|
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sub for Point3 {
|
||||||
|
type Output = Vector3;
|
||||||
|
|
||||||
|
fn sub(self, rhs: Point3) -> Self::Output {
|
||||||
|
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Plane {
|
||||||
|
point: Point3,
|
||||||
|
normal: Vector3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Plane {
|
||||||
|
pub fn new(point: Point3, normal: Vector3) -> Plane {
|
||||||
|
Plane { point, normal }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Line {
|
||||||
|
point: Point3,
|
||||||
|
direction: Vector3,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Sphere {
|
||||||
|
point: Point3,
|
||||||
|
radius: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sphere {
|
||||||
|
pub fn new(point: Point3, radius: f64) -> Sphere {
|
||||||
|
Sphere { point, radius }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum Geometry {
|
||||||
|
Sphere(Sphere),
|
||||||
|
Plane(Plane),
|
||||||
|
Line(Line),
|
||||||
|
}
|
||||||
147
src/main.rs
147
src/main.rs
@ -1,128 +1,4 @@
|
|||||||
use std::ops::{Add, Div, Mul, Sub};
|
use hello::*;
|
||||||
use std::collections::
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
struct Vector3 {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
z: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Vector3 {
|
|
||||||
fn new(x: f64, y: f64, z: f64) -> Vector3 {
|
|
||||||
Vector3 { x, y, z }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<f64> for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn mul(self, rhs: f64) -> Self::Output {
|
|
||||||
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul<Vector3> for f64 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Mul for Vector3 {
|
|
||||||
type Output = f64;
|
|
||||||
|
|
||||||
fn mul(self, rhs: Vector3) -> Self::Output {
|
|
||||||
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Div<f64> for Vector3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn div(self, rhs: f64) -> Self::Output {
|
|
||||||
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
struct Point3 {
|
|
||||||
x: f64,
|
|
||||||
y: f64,
|
|
||||||
z: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Point3 {
|
|
||||||
fn new(x: f64, y: f64, z: f64) -> Point3 {
|
|
||||||
Point3 { x, y, z }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Add<Vector3> for Point3 {
|
|
||||||
type Output = Point3;
|
|
||||||
|
|
||||||
fn add(self, rhs: Vector3) -> Self::Output {
|
|
||||||
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for Point3 {
|
|
||||||
type Output = Vector3;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Point3) -> Self::Output {
|
|
||||||
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Plane {
|
|
||||||
point: Point3,
|
|
||||||
normal: Vector3,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Plane {
|
|
||||||
fn new(point: Point3, normal: Vector3) -> Plane {
|
|
||||||
Plane { point, normal }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Line {
|
|
||||||
point: Point3,
|
|
||||||
direction: Vector3,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Sphere {
|
|
||||||
point: Point3,
|
|
||||||
radius: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sphere {
|
|
||||||
fn new(point: Point3, radius: f64) -> Sphere {
|
|
||||||
Sphere { point, radius }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Geometry {
|
|
||||||
Sphere(Sphere),
|
|
||||||
Plane(Plane),
|
|
||||||
Line(Line),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0);
|
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, 0.0), 25.0);
|
||||||
@ -145,4 +21,25 @@ fn main() {
|
|||||||
Geometry::Line(l) => println!("A Line"),
|
Geometry::Line(l) => println!("A Line"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut lottery = [4, 8, 15, 16, 23, 42];
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
|
||||||
|
for value in lottery {
|
||||||
|
sum += value;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("The sum is {}", sum);
|
||||||
|
|
||||||
|
let mut lottery_string = Vec::new();
|
||||||
|
for value in lottery {
|
||||||
|
lottery_string.push(value.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let lottery_string_2: Vec<String> = lottery
|
||||||
|
.iter()
|
||||||
|
.filter(|x| **x > 8)
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user