I wonder why the type parameter of std::ops::Index
is ?Sized
. Afterall, I can't implement it with an unsized type, because the mandatory index
method requires the index as an argument:
struct A;
impl std::ops::Index<str> for A {
type Output = ();
fn index(&self, index: str) -> &() {
&()
}
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> src/lib.rs:5:21
|
5 | fn index(&self, index: str) -> &() {
| ^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
help: function arguments must have a statically known size, borrowed types always have a known size
|
5 | fn index(&self, index: &str) -> &() {
| +
For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to previous error
Maybe this is to support certain language built-in indexing mechanisms that I can't reproduce in user code?