Skip to content

Commit

Permalink
add vector type which accepts at least one element
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao committed Sep 18, 2024
1 parent 58ee395 commit eded347
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2048,6 +2048,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 50. Custom Option methods impl

// #[allow(dead_code)]
// #[derive(Debug, PartialEq)]
// enum MyOption<T> {
Expand Down Expand Up @@ -2233,6 +2234,8 @@

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 51. Custom Result methods impl

// #[allow(dead_code)]
// #[derive(Debug)]
// pub enum MyResult<T, E> {
Expand Down Expand Up @@ -2313,4 +2316,68 @@

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

// 52. Vector with at least single element

// use std::fmt::Debug;

// struct Vector<T> {
// first: T,
// rest: Vec<T>,
// }

// #[macro_export]
// macro_rules! vec {
// ($first:expr) => {
// Vector::new($first)
// };

// ($first:expr, $($rest:expr),*) => {{
// let mut vector = Vector::new($first);
// $(
// vector.push($rest);
// )*
// vector
// }};
// }

// impl<T> Vector<T> {
// fn new(first: T) -> Self {
// Self {
// first,
// rest: Vec::default(),
// }
// }

// fn push(&mut self, element: T) {
// self.rest.push(element);
// }
// }

// impl<T: Debug + Clone> Debug for Vector<T> {
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// let first = self.first.clone();
// let mut vector = Vec::default();
// vector.push(first);
// vector.extend(self.rest.clone());

// Debug::fmt(&vector, f)
// }
// }

// fn main() {
// let mut vecc = vec![1];
// vecc.push(2);
// vecc.push(3);
// println!("{vecc:?}");

// let vecc = vec![1, 2];
// println!("{vecc:?}");

// // Error
// // let vecc = vec![];
// // println!("{vecc:?}");
// }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

fn main() {}

0 comments on commit eded347

Please sign in to comment.