I have a Rust function that panic
s under some condition and I wish to write a test case to validate whether the function is panicking or not. I couldn't find anything except the assert!
and assert_eq!
macros. Is there some mechanism for testing this?
I could spawn a new task and checking whether that task panics or not. Does it make sense?
Returning a Result<T, E>
is not suitable in my case.
I wish to add support for the Add
trait to a Matrix
type I am implementing. The ideal syntax for such addition would look like:
let m = m1 + m2 + m3;
where m1
, m2
, m3
are all matrices. Hence, the result type of add
should be Matrix
. Something like the following would be too cryptic:
let m = ((m1 + m2).unwrap() + m3).unwrap()
At the same time, the add()
function needs to validate that the two matrices being added have same dimension. Thus, add()
needs to panic if the dimensions don't match. The available option is panic!()
.