10 ms·
> - That unwrap should not be used in the kernel, correct? Yes. I guess the exception is if it will only panic on a kernel bug and you're ok with that (that's
by gpm 6y ago
> - That unwrap should not be used in the kernel, correct?
Yes.
I guess the exception is if it will only panic on a kernel bug and you're ok with that (that's what the bug macro is for, no)? E.g. if I have `let x = Some(2); let y = x.unwrap();`. The second statement panics if x is None, but that would only happen in the event of a bug, so you might be ok with it.
This does happen sometimes in real code, `if some_list.len() > 2 { let x = some_list.pop().unwrap() }` will never panic, because pop() only returns None (think null) if the list is empty, but we just checked it has at least two elements. I'm not sure what the kernels stance on this sort of code will be.
> - That I should be very careful about the external libraries I use and look for the presence of panic! and/or unwrap,
In the context of the kernel, I imagine you just don't use external libraries at all. In a more general context, yes, you need to understand when any libraries you use might panic. The general convention that is honestly not very well followed is to document panics that result from usage errors, and to allow for panics caused by library bugs.