Skip to main content

hello_foundry/
lib.rs

1pub struct Hello {
2    greeting: String,
3}
4
5impl Hello {
6    /// Creates a new Hello instance with the default greeting.
7    pub fn new() -> Self {
8        Self {
9            greeting: "Hello, World!".to_string(),
10        }
11    }
12
13    /// Sets a new greeting.
14    pub fn set_greeting(&mut self, new_greeting: impl Into<String>) {
15        self.greeting = new_greeting.into();
16    }
17
18    /// Gets the current greeting.
19    pub fn get_greeting(&self) -> &str {
20        &self.greeting
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27
28    #[test]
29    fn default_greeting() {
30        let hello = Hello::new();
31        assert_eq!(hello.get_greeting(), "Hello, World!");
32    }
33
34    #[test]
35    fn set_and_get_greeting() {
36        let mut hello = Hello::new();
37        hello.set_greeting("Hi, Rust!");
38        assert_eq!(hello.get_greeting(), "Hi, Rust!");
39    }
40}