質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

Q&A

解決済

2回答

2389閲覧

into() 呼び出し時の「type annotations needed 」解消方法

sutonea

総合スコア207

Rust

Rustは、MoFoが支援するプログラミング言語。高速性を維持しつつも、メモリ管理を安全に行うことが可能な言語です。同じコンパイル言語であるC言語やC++では困難だったマルチスレッドを実装しやすく、並行性という点においても優れています。

0グッド

0クリップ

投稿2021/09/23 04:35

前提・実現したいこと

iced を用いて、macOSネイティブアプリの作り方を調査しています。
現在、文字列の配列を、画面上にTextとして表示しようとしています。

Column を生成し、そこへ 複数の Text を追加した後
into() で表示したいのですが、コンパイルエラーとなっています。

コンパイルエラーの解消方法を教えていただけませんでしょうか?

発生している問題・エラーメッセージ

error[E0282]: type annotations needed --> src/main.rs:50:14 | 50 | *col.into() | ----^^^^-- | | | | | cannot infer type for type parameter `T` declared on the trait `Into` | this method call resolves to `T` | = note: type must be known at this point

該当のソースコード

rust

1use iced::{Settings, Application, Command, Clipboard, Element, Column, Text}; 2 3fn main() -> iced::Result { 4 TextViewer::run(Settings::default()) 5} 6 7#[derive(Debug, Clone)] 8struct TextViewer {} 9 10#[derive(Debug)] 11pub enum Message { 12 Loading, 13 Loaded, 14} 15 16impl Application for TextViewer { 17 type Executor = iced::executor::Default; 18 type Message = Message; 19 type Flags = (); 20 21 fn new(_flags: Self::Flags) -> (Self, Command<Self::Message>) { 22 (TextViewer{}, Command::none()) 23 } 24 25 fn title(&self) -> String { 26 String::from("Text を複数表示する実験") 27 } 28 29 fn update(&mut self, message: Self::Message, _clipboard: &mut Clipboard) -> Command<Self::Message> { 30 match message { 31 Message::Loading => {} 32 Message::Loaded => {} 33 } 34 Command::none() 35 } 36 37 fn view(&mut self) -> Element<'_, Self::Message> { 38 let listString = [String::from("ABC"), String::from("DEF")]; 39 let col = &Column::new(); 40 for s in listString.iter() { 41 col.push(Text::new(s)); 42 } 43 44 45 // 参照外ししない場合 46 // col.into() 47 // ^^^^ the trait `From<&iced_native::widget::column::Column<'_, _, iced_graphics::renderer::Renderer<iced_wgpu::backend::Backend>>>` is not implemented for `iced_native::element::Element<'_, Message, iced_graphics::renderer::Renderer<iced_wgpu::backend::Backend>>` 48 49 50 // 参照外しする場合 51 *col.into() 52// ----^^^^-- 53// | | | 54// | | cannot infer type for type parameter `T` declared on the trait `Into` 55// | this method call resolves to `T` 56 57 } 58}

試したこと

view 関数内部で Box を利用してみましたが、同様のエラーが発生しました。

rust

1fn view(&mut self) -> Element<'_, Self::Message> { 2 let listString = [String::from("ABC"), String::from("DEF")]; 3 let col = Box::new(Column::new()); 4 for s in listString.iter() { 5 col.push(Text::new(s)); 6 } 7 8 *col.into() 9// ----^^^^-- 10// | | | 11// | | cannot infer type for type parameter `T` declared on the trait `Into` 12// | this method call resolves to `T` 13 }

補足情報(FW/ツールのバージョンなど)

OS

MacOS Big Sur 11.5.2(20G95)

Cargo.toml

[package] name = "RustWithCLion" version = "0.1.0" edition = "2018" publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] iced = { version = "0.3", features = ["async-std", "debug"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0"

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

こちらですが、colの参照外ししてからinto()することを狙っているのだと思いますが、意図どおりになっていません。

rust

1 // 参照外しする場合 2 *col.into() 3// ----^^^^-- 4// | | | 5// | | cannot infer type for type parameter `T` declared on the trait `Into` 6// | this method call resolves to `T`

単項の*演算子の優先順位はメソッド呼び出し.into()の優先順位よりも低いので、以下のようにカッコ付きで書いたのと同じになります。(Rust Reference — Expression precedence

rust

1 // colをinto()してから参照外しする 2 *(col.into())

意図どおりにするには、以下のように書く必要があります。

rust

1 // colを参照外ししてからinto()する 2 (*col).into()

しかし、これは別のコンパイルエラーになります。

console

1cannot move out of `*col` which is behind a shared reference

これはcol&Column<_, _>型なことが原因です。

元のコード

rust

1 fn view(&mut self) -> Element<'_, Self::Message> { 2 let listString = [String::from("ABC"), String::from("DEF")]; 3 // colは&Column<_, _>型 4 let col = &Column::new(); 5 for s in listString.iter() { 6 col.push(Text::new(s)); 7 } 8 // 参照外しできない(cannot move out ...) 9 (*col).into() 10 }

そこで、以下のようにcolColumn<_, _>型に変えてみたところコンパイルできました。これで解決したと思います。

rust

1 fn view(&mut self) -> Element<'_, Self::Message> { 2 let listString = [String::from("ABC"), String::from("DEF")]; 3 // colをColumn<_, _>型にする 4 let mut col = Column::new(); 5 for s in listString.iter() { 6 // pushはColumn<_, _>の所有権を取り、新しいColumn<_, _>を返す 7 col = col.push(Text::new(s)); 8 } 9 // Column<_, _>なので参照外しは不要 10 col.into() 11 }

追記
すでに同じような内容の回答が付いていることに気づかずに自分の回答を投稿してしまいました。そちらで紹介されているColumn::with_children()を使うのが便利そうですね。

投稿2021/09/23 08:17

編集2021/09/23 08:37
tatsuya6502

総合スコア2035

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sutonea

2021/09/23 08:47

演算子の優先順位など、細かく記載いただきありがとうございます! 理解が深まりました!
guest

0

ベストアンサー

ぱっと見, iced::Columnの生成の問題なのかなと思います.

同様の形でやっていくのであれば, Columnの生成からpushのループを以下のようにすると, col.into() が通るようになるかなと思います.

rust

1let mut col = Column::new(); 2for s in listString.iter() { 3 col = col.push(Text::new(s)); 4}

自分でforなどでiter()を回すのも面倒かなと言う場合は, Column::with_children() を利用して,

rust

1fn view(&mut self) -> Element<'_, Self::Message> { 2 let texts = vec![Text::new("ABC").into(), Text::new("DEF").into()]; 3 Column::with_children(texts).into() 4}

としても大丈夫かと存じます.

投稿2021/09/23 07:44

編集2021/09/23 07:46
m13o

総合スコア22

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sutonea

2021/09/23 08:45

ありがとうございます、そちらの方法で解決いたしました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問