api-design

Why does the Duration class not have 'toSeconds()' method?

只愿长相守 提交于 2020-12-29 08:45:48
问题 I was looking at the Duration class in Java 8 and noticed that it does not have: long toSeconds(); But it has all other toXXXXX() to get days, hours, minutes, millis, nanos. I do see a getSeconds() method that returns the number of seconds within this duration object. There is also a get(TemporalUnit unit) method to get the duration as the requested time unit. But why not keep the toSeconds() method for consistency? 回答1: Let's look at what the docs say: This class models a quantity or amount

Using the Rust compiler to prevent forgetting to call a method

。_饼干妹妹 提交于 2020-11-30 12:17:10
问题 I have some code like this: foo.move_right_by(10); //do some stuff foo.move_left_by(10); It's really important that I perform both of those operations eventually , but I often forget to do the second one after the first. It causes a lot of bugs and I'm wondering if there is an idiomatic Rust way to avoid this problem. Is there a way to get the rust compiler to let me know when I forget? My idea was to maybe somehow have something like this: // must_use will prevent us from forgetting this if