Skip to content

Instantly share code, notes, and snippets.

@NaitYoussef
Created December 12, 2024 19:56
Show Gist options
  • Save NaitYoussef/204265b227b90a7aaf9440710712bede to your computer and use it in GitHub Desktop.
Save NaitYoussef/204265b227b90a7aaf9440710712bede to your computer and use it in GitHub Desktop.
use crate::Optional::FULL;
use chrono::{DateTime, Utc};
use std::fmt::Debug;
use std::marker::PhantomData;
mod bank_account;
const papa: u32 = 200;
#[derive(Debug)]
struct Person {
name: String,
age: u8,
}
impl Person {
fn new(name: String, age: u8) -> Result<Person, String> {
if age > 100 {
return Err(String::from("Age must be between less than 100."));
}
Ok(Person { name, age })
}
}
enum Transaction {
Deposit(DateTime<Utc>),
Withdraw(DateTime<Utc>, u32),
}
impl Transaction {
fn printTrans(&self) {
match self {
Transaction::Deposit(_) => println!("deposit"),
Transaction::Withdraw(_, _) => println!("WithDraw"),
}
}
}
trait Genre: Debug {
fn genre(&self) -> String;
fn printGenre(&self) {
println!("{:?}", self);
}
}
#[derive(Debug)]
struct Person2(String, u8);
impl Genre for Person2 {
fn genre(&self) -> String {
String::from("TITI")
}
}
impl Genre for String {
fn genre(&self) -> String {
String::from("String yeah")
}
}
#[derive(Debug)]
enum Optional<T>
where
T: Debug,
{
EMPTY(),
FULL(T),
}
impl<T> Optional<T>
where
T: Debug,
{
fn isEmpty(&self) -> bool {
println!("{:?}", &self);
let x = match self {
EMPTY => {
println!("EMPTY {:?}", self);
true
}
FULL(_) => {
println!("FULL");
false
}
};
println!("x is {x}");
x
}
}
impl Optional<u32> {
fn isEmpty2(&self) -> bool {
let x = match self {
EMPTY => {
println!("EMPTY u32 {:?}", self);
true
}
FULL(_) => {
println!("FULL u32");
false
}
};
x
}
}
struct Person3<T> {
name: String,
age: u8,
fantom: PhantomData<T>,
}
struct Homme {}
struct Femme {}
impl Person3<Homme> {
fn printMe(&self) {
println!("Je suis un homme");
}
}
impl Person3<Femme> {
fn printMe(&self) {
println!("Je suis une femme");
}
}
type void = (); //alias
fn printAge(person: &Person) {
println!("Age is {}.", person.age);
}
impl Person {
fn printAge(&self) {
println!("Age is {}.", self.age);
}
}
fn printName(person: &Person) -> void {
println!("Name is {}.", person.name);
}
fn returnAge(person: &Person) -> u8 {
person.age
}
fn setAge(person: &mut Person, age: u8) {
person.age = age;
}
fn main() -> Result<void, String> {
/*let mut account = BankAccount::create_new_account("0987654323".to_string(), 2000);
account.deposit(100);
account.deposit(300);
account.withdraw(150);
println!("{account}");
*/
let per = Person::new(String::from("TITI"), 200)?;
let s = String::from("Hello");
s.genre();
let homme: Person3<Femme> = Person3 {
age: 32,
name: String::from("tp"),
fantom: Default::default(),
};
homme.printMe();
let t = Transaction::Deposit(Utc::now());
t.printTrans();
let mut a = vec![31];
for i in &a {
println!("{}", i);
}
let mut b = Person {
name: String::from("John"),
age: 33,
};
let op: Optional<Person> = Optional::FULL(b);
println!("titi {}", op.isEmpty());
let op2: Optional<u32> = Optional::FULL(32);
println!("titi {}", op2.isEmpty());
let x = Person2(String::from("John"), 33);
let c = (String::from("John"), 33);
//b.age = 20;
/*b.printAge();
printName(&b);
let age = returnAge(&b);
println!("Age is {}.", age);
setAge(&mut b, 29);
println!("Age is {}.", b.age);*/
let mut tab = [12];
tab[0] = 2;
a.push(23);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment