3 ms·
If they don't pass a number at all, looks like cx.argument() will return a Result::Err (which in the example will panic because of the unwrap(), but can instead
by brundolf 5y ago
If they don't pass a number at all, looks like cx.argument() will return a Result::Err (which in the example will panic because of the unwrap(), but can instead be bubbled up as another commenter pointed out here: https://news.ycombinator.com/item?id=28977697 https://news.ycombinator.com/item?id=28977697)
JsNumber::value() then returns an f64 to encapsulate all possible JS numbers, and `as i32` is just casting that float into an int. The exact behavior for this cast is that a) any fractional component will be dropped, and b) if the number is larger than the destination type, the destination's maximum value will be used. Both of these behaviors could of course be controlled by adding logic, like an explicit call to .round()