Strange Issue with Function Pointers: Generic Functions Can Be Assigned to Variables but Not to Struct Fields
💡
原文英文,约200词,阅读约需1分钟。
📝
内容提要
讲述了一个泛型函数 should_not_call 可以被赋值给变量 write_u64,但无法赋值给 d.write_u64 的情况。其中,write_le 函数可以被赋值给 write_u64,而 should_not_call 函数会返回一个错误。
🎯
关键要点
- should_not_call 是一个泛型函数,返回一个错误。
- write_u64 变量可以被赋值为 write_le 函数。
- 在 ok 函数中,write_u64 可以被赋值为 should_not_call。
- 在 failed 函数中,d.write_u64 不能被赋值为 should_not_call。
- Dump 结构体包含一个 Write trait 的实例和一个函数指针 write_u64。
➡️