Created
October 16, 2024 19:54
-
-
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/8f883ec44eeea85e72eb4c5526e5fa1c9a294c70
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
#!/usr/bin/env rust-script | |
// summary : hello rust structs and methods | |
// keywords : rust, structs, data-class, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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