MCPcopy Index your code
hub / github.com/derekbanas/Rust-Tutorial / main

Function main

main.rs:161–1100  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

159}
160
161fn main() {
162 // It is common to indent with 4 spaces
163 // You can tell println is a macro because of the !
164 // and not a function
165 println!("What is your name?");
166
167 // Define an mutable variable (Value can changed)
168 // String::new : A function that returns an empty string
169 let mut name = String::new();
170
171 /*
172 By default variables are immutable (Value can't Change)
173 but it is possible to use mutable variables
174 It is beneficial to use immutable variables because then
175 you don't have to track down how values change
176 */
177
178 // This string is immutable
179 // Define it is a string with double quotes
180 let greeting = "Nice to meet you";
181
182 /*
183 Receive input from the user with read_line
184 name is passed as an argument to read_line
185 & defines that this variable is a reference to the variable
186 which allows read_line to save values directly to name
187 You use mut to define that name is a mutable variable
188 */
189
190 /*
191 read_line returns io::Result which is an enum
192 Enums have a fixed number of specific values (Ok or Err)
193 If Err is returned the operation failed and Err can tell you why
194 Result has an expect function that returns any error that occurred
195 (We should handle this error, but we'll cover that later)
196 */
197 io::stdin().read_line(&mut name)
198 .expect("Didn't Receive Input");
199
200 // Were you have {} your variable values will be placed
201 // To remove the newline after name use trim_end
202 println!("Hello {}! {}", name.trim_end(), greeting);
203
204 // ----- VARIABLES -----
205
206 // Constants can be declared in any scope and used globally
207 // They differ from immutable variables in that their value
208 // can't be defined at runtime (based on a function call for example)
209 const ONE_MIL: u32 = 1_000_000;
210 const PI: f32 = 3.141592;
211
212 // You can define variables with the same name but with different
213 // data types (Shadowing)
214 let age = "47";
215
216 // Trim eliminates white space and parse converts the string into an int
217 // We expect age to have an integer value and expect will throw an
218 // error if this isn't true (We'll get more into error handling later)

Callers

nothing calls this directly

Calls 10

say_helloFunction · 0.85
get_sumFunction · 0.85
get_2Function · 0.85
print_strFunction · 0.85
print_return_strFunction · 0.85
change_stringFunction · 0.85
order_foodFunction · 0.85
customerFunction · 0.85
rightMethod · 0.80
leftMethod · 0.80

Tested by

no test coverage detected