mirror of
https://github.com/KaiserY/trpl-zh-cn
synced 2025-04-23 14:18:13 +08:00
35 lines
700 B
Rust
35 lines
700 B
Rust
|
pub struct Guess {
|
||
|
value: i32,
|
||
|
}
|
||
|
|
||
|
impl Guess {
|
||
|
pub fn new(value: i32) -> Guess {
|
||
|
// ANCHOR: here
|
||
|
if value < 1 {
|
||
|
panic!(
|
||
|
"Guess value must be less than or equal to 100, got {}.",
|
||
|
value
|
||
|
);
|
||
|
} else if value > 100 {
|
||
|
panic!(
|
||
|
"Guess value must be greater than or equal to 1, got {}.",
|
||
|
value
|
||
|
);
|
||
|
}
|
||
|
// ANCHOR_END: here
|
||
|
|
||
|
Guess { value }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
#[should_panic(expected = "Guess value must be less than or equal to 100")]
|
||
|
fn greater_than_100() {
|
||
|
Guess::new(200);
|
||
|
}
|
||
|
}
|