## 事象
`Gtfs`は[[トレイト]]で、それを返却する以下のようなメソッドがあるとき。
```rust
pub fn create(path: &PathBuf) -> Result<Gtfs> {
let ins = GtfsDb::new(path)?;
Ok(ins)
}
```
以下のエラーになる。
```rust
warning: trait objects without an explicit `dyn` are deprecated
--> src\external\gtfs.rs:18:41
|
18 | pub fn create(path: &PathBuf) -> Result<Gtfs> {
| ^^^^ help: use `dyn`: `dyn Gtfs`
|
= note: `#[warn(bare_trait_objects)]` on by default
error[E0277]: the size for values of type `(dyn Gtfs + 'static)` cannot be known at compilation time
--> src\external\gtfs.rs:18:34
|
18 | pub fn create(path: &PathBuf) -> Result<Gtfs> {
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
::: C:\Users\syoum\scoop\persist\rustup\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\result.rs:241:17
|
241 | pub enum Result<T, E> {
| - required by this bound in `std::result::Result`
|
= help: the trait `Sized` is not implemented for `(dyn Gtfs + 'static)`
```
## 原因
warningとerrorのそれぞれに原因がある。
### warningについて
```
warning: trait objects without an explicit `dyn` are deprecated
```
[[トレイト]]の前に[[®dyn]]を明示的につけていないため。
### errorについて
```
error[E0277]: the size for values of type `(dyn Gtfs + 'static)` cannot be known at compilation time
```
コンパイル時に`Gtfs`のサイズが分からないためエラーになる。
## 対策1
原因に対して正直に対応する場合。[[Box]]をつけてHeapを使えばよい。[[®dyn]]もつける。
```rust
pub fn create(path: &PathBuf) -> Result<Box<dyn Gtfs>> {
let ins = GtfsDb::new(path)?;
Ok(Box::new(ins))
}
```
## 対策2
`impl`を使う。この場合、呼び出し元で`use Gtfs`が必要になるので注意。
```rust
pub fn create(path: &PathBuf) -> Result<impl Gtfs> {
let ins = GtfsDb::new(path)?;
Ok(ins)
}
```
対策1との違いは[[®dyn#GenericやImplキーワードとの比較]]を参照。