Skip to content

Commit

Permalink
feat: add hello-world example for using rust
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Dec 17, 2023
1 parent e5664da commit 36fd5e4
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hello-world/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
67 changes: 67 additions & 0 deletions hello-world/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::fmt;

#[derive(Debug)]
struct Person<T: AsRef<str>> {
name: T,
age: u8,
}

// T is the inner type of the Person to store the name, here
// we want it to have an implementation for display.
impl<T: AsRef<str> + fmt::Display> fmt::Display for Person<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
return write!(
f,
"There is a person with name: {} and age {}",
self.name, self.age
);
}
}

fn main() {
println!("Hello, world!, because it is a hello world example");

let _h: &str = "Hello";

// create a person using an AsRef
let mut p = Person {
name: "Parham Alvani",
age: 25,
};

println!("{}", p);

{
let name: String = String::from("Elahe Dastan");
p.name = name.as_str();

println!("{}", p);
}

// uncomment the following line to get a nice compile error
// because it __moves__ into inner context and name doesn't live
// enough
// println!("{}", p);

let mut p = Person {
name: String::from("Raha Dastan"),
age: 23,
};

p.name.push('!');

println!("{}", p);

// learn about clourses
let p = Person {
name: "Parham Alvani",
age: 25,
};

let parham_namer = || {
println!("{}", p.name);
};

parham_namer();
parham_namer();
}

0 comments on commit 36fd5e4

Please sign in to comment.