Last active
February 3, 2026 20:20
-
-
Save dacr/19c2856ee9218f2ed43f5c75f17eb059 to your computer and use it in GitHub Desktop.
hello rust structs and methods / published by https://github.com/dacr/code-examples-manager #0d42efab-2b25-4c42-84a4-6d75be03f195/8bef2f7ce2b09d5907b14e31c0ec8090fc64e8cc
This file contains hidden or 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
| #!/usr/bin/env rust-script | |
| // summary : hello rust structs and methods | |
| // keywords : rust, structs, data-class, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 0d42efab-2b25-4c42-84a4-6d75be03f195 | |
| // created-on : 2024-10-16T09:05:31+02:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : ./$file | |
| struct Someone { | |
| name: String, | |
| age: Option<u8>, | |
| } | |
| impl Someone { | |
| fn new(name: String) -> Self { | |
| Self { name, age: None } | |
| } | |
| fn greetings(&self) { // comment on prend possession l'instance // ICI en immutable | |
| match self.age { | |
| Some(age) => println!("Hello, {} year old named {}!", age, self.name), | |
| None => println!("Hello, {} with year old unknown!", self.name), | |
| } | |
| } | |
| } | |
| fn main() { | |
| let comp = Someone::new(String::from("Rust 🚀")); | |
| comp.greetings(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment