-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hello-world example for using rust
- Loading branch information
1 parent
e5664da
commit 36fd5e4
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |