xref: /aosp_15_r20/build/bazel/examples/rust/src/greeter.rs (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1 // Copyright (C) 2023 The Android Open Source Project
2 
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /// Object that displays a greeting.
16 pub struct Greeter {
17     greeting: String,
18 }
19 
20 /// Implementation of Greeter.
21 impl Greeter {
22     /// Constructs a new `Greeter`.
23     ///
24     /// # Examples
25     ///
26     /// ```
27     /// use hello_lib::greeter::Greeter;
28     ///
29     /// let greeter = Greeter::new("Hello");
30     /// ```
new(greeting: &str) -> Greeter31     pub fn new(greeting: &str) -> Greeter {
32         Greeter { greeting: greeting.to_string() }
33     }
34 
35     /// Returns the greeting as a string.
36     ///
37     /// # Examples
38     ///
39     /// ```
40     /// use hello_lib::greeter::Greeter;
41     ///
42     /// let greeter = Greeter::new("Hello");
43     /// let greeting = greeter.greeting("World");
44     /// ```
greeting(&self, thing: &str) -> String45     pub fn greeting(&self, thing: &str) -> String {
46         format!("{} {}", &self.greeting, thing)
47     }
48 
49     /// Prints the greeting.
50     ///
51     /// # Examples
52     ///
53     /// ```
54     /// use hello_lib::greeter::Greeter;
55     ///
56     /// let greeter = Greeter::new("Hello");
57     /// greeter.greet("World");
58     /// ```
greet(&self, thing: &str)59     pub fn greet(&self, thing: &str) {
60         println!("{} {}", &self.greeting, thing);
61     }
62 }
63