Skip to content

Commit 1f07fd4

Browse files
committed
Auto merge of #163 - briankung:add_structs, r=komaeda
Adds a simple exercise for structures Thanks for rustlings! Here's a small contribution in return.
2 parents 8bf8cbb + 9b92aa0 commit 1f07fd4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

exercises/structs/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Strings
2+
3+
Rust has three struct types: a classic c struct, a tuple struct, and a unit struct.
4+
5+
#### Book Sections
6+
7+
- [Structures](https://doc.rust-lang.org/rust-by-example/custom_types/structs.html)

exercises/structs/structs1.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// structs1.rs
2+
// Address all the TODOs to make the tests pass!
3+
4+
struct ColorClassicStruct {
5+
// TODO: Something goes here
6+
}
7+
8+
struct ColorTupleStruct(/* TODO: Something goes here */);
9+
10+
struct ColorUnitStruct;
11+
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
16+
#[test]
17+
fn classic_c_structs() {
18+
// TODO: Instantiate a classic c struct!
19+
// let green =
20+
21+
assert_eq!(green.name, "green");
22+
assert_eq!(green.hex, "#00FF00");
23+
}
24+
25+
#[test]
26+
fn tuple_structs() {
27+
// TODO: Instantiate a tuple struct!
28+
// For more fun, use the field initialization shorthand.
29+
// let green =
30+
31+
assert_eq!(green.0, "green");
32+
assert_eq!(green.1, "#00FF00");
33+
}
34+
35+
#[test]
36+
fn unit_structs() {
37+
// TODO: Instantiate a unit struct!
38+
// let green =
39+
40+
if let ColorUnitStruct = green {
41+
assert!(true);
42+
} else {
43+
assert!(false);
44+
}
45+
}
46+
}

info.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ mode = "compile"
7676
path = "exercises/primitive_types/primitive_types6.rs"
7777
mode = "compile"
7878

79+
# STRUCTS
80+
81+
[[exercises]]
82+
path = "exercises/structs/structs1.rs"
83+
mode = "test"
84+
7985
# TESTS
8086

8187
[[exercises]]

0 commit comments

Comments
 (0)