Skip to content

Commit 4059b20

Browse files
committed
docco: added struct and arg input
1 parent 25897cb commit 4059b20

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ You can destructure values and assign like this:
137137
let (var1, var2) = (1, 2);
138138
```
139139

140+
## Structs
141+
142+
Structs are types that you define. You can access the attributes of the data type using dot notation.
143+
144+
```rust
145+
struct User {
146+
name: String,
147+
email: String,
148+
age: i32
149+
}
150+
151+
fn main() {
152+
let bob = User{
153+
name: "Bob".to_string(),
154+
email: "[email protected]".to_string(),
155+
age: 32
156+
};
157+
158+
println!("Bob is: {}, {}, {}", bob.name, bob.email, bob.age);
159+
}
160+
```
161+
140162
## Help from the Compiler
141163

142164
When you get compile errors, Rust gives you a handy explain command: `rustc --explain E0384`. Run that to get more information on the error. Pretty useful stuff.
@@ -199,11 +221,29 @@ https://crates.io/
199221

200222
## Command line
201223

224+
To access args you can use `std::env::args()`. This gets them once the program is called:
225+
202226
```rust
203227
let args: Vec<String> = std::env::args().skip(1).collect();
204228
println!("{:?}", args);
205229
```
206230

231+
This loops over input:
232+
233+
```rust
234+
use std::io;
235+
use std::io::prelude::*;
236+
237+
fn main() {
238+
let stdin = io::stdin();
239+
for line in stdin.lock().lines() {
240+
let input = line.unwrap();
241+
println!("you entered {}", input);
242+
}
243+
}
244+
```
245+
246+
207247
## Resources
208248

209249
- [Actix api framework](https://actix.rs)

0 commit comments

Comments
 (0)