mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-10-22 00:00:03 +03:00
Compare commits
13 commits
03bf233374
...
913a906fe7
Author | SHA1 | Date | |
---|---|---|---|
913a906fe7 | |||
c7a18b0781 | |||
01a78531ad | |||
c2414b8891 | |||
f03020a7e2 | |||
01509a2a84 | |||
66b3a9cdd7 | |||
baca8c9667 | |||
8c3b8dcec4 | |||
881d3e9441 | |||
4eec81a113 | |||
9a13bccd63 | |||
40e2330e8d |
9
.github/workflows/rust.yml
vendored
9
.github/workflows/rust.yml
vendored
|
@ -2,14 +2,19 @@ name: Rustlings Tests
|
|||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
branches: [main]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
clippy:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- run: cargo clippy -- --deny warnings
|
||||
fmt:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
// A basket of fruits in the form of a hash map needs to be defined. The key
|
||||
// represents the name of the fruit and the value represents how many of that
|
||||
// particular fruit is in the basket. You have to put at least three different
|
||||
// types of fruits (e.g apple, banana, mango) in the basket and the total count
|
||||
// types of fruits (e.g. apple, banana, mango) in the basket and the total count
|
||||
// of all the fruits should be at least five.
|
||||
//
|
||||
// Make me compile and pass the tests!
|
||||
|
|
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);
|
||||
}
|
||||
}
|
18
flake.lock
18
flake.lock
|
@ -3,11 +3,11 @@
|
|||
"flake-compat": {
|
||||
"flake": false,
|
||||
"locked": {
|
||||
"lastModified": 1673956053,
|
||||
"narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=",
|
||||
"lastModified": 1696426674,
|
||||
"narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=",
|
||||
"owner": "edolstra",
|
||||
"repo": "flake-compat",
|
||||
"rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9",
|
||||
"rev": "0f9255e01c2351cc7d116c072cb317785dd33b33",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -21,11 +21,11 @@
|
|||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1692799911,
|
||||
"narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=",
|
||||
"lastModified": 1710146030,
|
||||
"narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44",
|
||||
"rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
@ -36,11 +36,11 @@
|
|||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1694183432,
|
||||
"narHash": "sha256-YyPGNapgZNNj51ylQMw9lAgvxtM2ai1HZVUu3GS8Fng=",
|
||||
"lastModified": 1715447595,
|
||||
"narHash": "sha256-VsVAUQOj/cS1LCOmMjAGeRksXIAdPnFIjCQ0XLkCsT0=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "db9208ab987cdeeedf78ad9b4cf3c55f5ebd269b",
|
||||
"rev": "062ca2a9370a27a35c524dc82d540e6e9824b652",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
12
info.toml
12
info.toml
|
@ -378,7 +378,7 @@ path = "exercises/06_move_semantics/move_semantics5.rs"
|
|||
mode = "test"
|
||||
hint = """
|
||||
Carefully reason about the range in which each mutable reference is in
|
||||
scope. Does it help to update the value of referent (`x`) immediately after
|
||||
scope. Does it help to update the value of `x` immediately after
|
||||
the mutable reference is taken? Read more about 'Mutable References'
|
||||
in the book's section 'References and Borrowing':
|
||||
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html#mutable-references.
|
||||
|
@ -451,6 +451,16 @@ the `Package` struct that this relates to?
|
|||
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
|
||||
|
||||
[[exercises]]
|
||||
|
|
Loading…
Reference in a new issue