mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 15:26:29 +00:00
feat: add structs4.rs exercise
This commit is contained in:
parent
3963559810
commit
40e2330e8d
2 changed files with 72 additions and 0 deletions
62
exercises/structs/structs4.rs
Normal file
62
exercises/structs/structs4.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
// structs4.rs
|
||||||
|
// Structs can have methods and the first parameter is always self. In this exercise
|
||||||
|
// we have defined the Planet struct and we want to test some logic attached to it,
|
||||||
|
// make the code compile and the tests pass! If you have issues execute `rustlings hint structs4`
|
||||||
|
|
||||||
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Planet {
|
||||||
|
has_life: bool,
|
||||||
|
radius : u32
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Planet {
|
||||||
|
fn new(radius: u32) -> Planet {
|
||||||
|
// Something goes here...
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_life(???) -> bool {
|
||||||
|
// Something goes here...
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change_radius(???) {
|
||||||
|
// Something goes here...
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_life(???) {
|
||||||
|
// Something goes here...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_planet() {
|
||||||
|
|
||||||
|
let planet = Planet::new(1000);
|
||||||
|
|
||||||
|
assert_eq!(planet.radius, 1000);
|
||||||
|
assert_eq!(planet.has_life(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_life_to_planet() {
|
||||||
|
let planet = Planet::new(1000);
|
||||||
|
|
||||||
|
planet.create_life();
|
||||||
|
|
||||||
|
assert_eq!(planet.has_life(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn change_radius_of_planet() {
|
||||||
|
let mut planet = Planet::new(1000);
|
||||||
|
|
||||||
|
planet.change_radius(2000);
|
||||||
|
|
||||||
|
assert_eq!(planet.radius, 2000);
|
||||||
|
}
|
||||||
|
}
|
10
info.toml
10
info.toml
|
@ -394,6 +394,16 @@ For calculate_transport_fees: Bigger is more expensive usually, we don't have si
|
||||||
|
|
||||||
Have a look in The Book, to find out more about method implementations: https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
|
Have a look in The Book, to find out more about method implementations: https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
|
||||||
|
|
||||||
|
[[exercises]]
|
||||||
|
name = "structs4"
|
||||||
|
path = "exercises/structs/structs4.rs"
|
||||||
|
mode = "test"
|
||||||
|
hint = """
|
||||||
|
The signature of methods must be different depending on the fact that attributes of an instance are
|
||||||
|
read or written. How do we do that in Rust?
|
||||||
|
|
||||||
|
Have a look in The Book, to find out more about method implementations: https://doc.rust-lang.org/book/ch05-03-method-syntax.html"""
|
||||||
|
|
||||||
# ENUMS
|
# ENUMS
|
||||||
|
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
|
|
Loading…
Reference in a new issue