fork download
  1.  
  2. #[derive(Debug)]
  3. struct Hoge {
  4. id: usize,
  5. ok: bool,
  6. }
  7.  
  8. impl Drop for Hoge {
  9. fn drop(&mut self) {
  10. assert!(self.ok, "must call .ok() (id: {})", self.id);
  11. }
  12. }
  13.  
  14. impl Hoge {
  15. fn new(id: usize) -> Self {
  16. Self{ id, ok: false }
  17. }
  18. fn next(mut self, check: i32) -> Result<Self,()> {
  19. self.ok = true;
  20. if check < 10 {
  21. Ok(Self{id: self.id, ok: false})
  22. } else {
  23. Err(())
  24. }
  25. }
  26. fn ok(mut self, check: i32) -> Result<(),()> {
  27. self.ok = true;
  28. if check < 10 {
  29. Ok(())
  30. } else {
  31. Err(())
  32. }
  33. }
  34. }
  35.  
  36. fn main() {
  37. run().unwrap();
  38. }
  39.  
  40. fn run() -> Result<(),()> {
  41. Hoge::new(1).next(5)?.next(8)?.next(4)?.ok(3)?;
  42. Hoge::new(2).next(1)?.next(2)?;
  43. Hoge::new(3).next(1)?.next(2)?.ok(3)?;
  44. Hoge::new(4).next(1)?.next(2)?.ok(5)
  45. }
Runtime error #stdin #stdout #stderr 0s 5640KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
thread 'main' panicked at 'must call .ok() (id: 2)', prog.rs:10:9
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.