Geob-o-matic
Geob-o-matic

Reputation: 6079

How do we convert a Rust string to a gtk::type::String?

I'm trying to create a ComboBox, especially the model for it:

let type_in_col = &[gtk::Type::String];
let list_model = ListStore::new(type_in_col);
list_model.insert_with_values(None, &[0], &[""]);
list_model.insert_with_values(None, &[0], &["h"]);
list_model.insert_with_values(None, &[0], &["H"]);
list_model.insert_with_values(None, &[0], &["W"]);
list_model.insert_with_values(None, &[0], &["S"]);

This piece of code gave me this error:

error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied
--> src\widgets\daywidget.rs:36:1
   |
36 | #[widget]
   | ^^^^^^^^^ `str` does not have a constant size known at compile-time
   |
= help: the trait `std::marker::Sized` is not implemented for `str`
= note: required for the cast to the object type `gtk::ToValue`

(the error is not very precise because I'm using Relm)

Upvotes: 3

Views: 400

Answers (1)

antoyo
antoyo

Reputation: 11923

You want to use the following, instead:

let type_in_col = &[gtk::Type::String];
let list_model = ListStore::new(type_in_col);
list_model.insert_with_values(None, &[0], &[&""]);
list_model.insert_with_values(None, &[0], &[&"h"]);
list_model.insert_with_values(None, &[0], &[&"H"]);
list_model.insert_with_values(None, &[0], &[&"W"]);
list_model.insert_with_values(None, &[0], &[&"S"]);

since SetValue is implemented for &T where T: ?Sized.

You cannot cast from &str to &ToValue: see here for the reasons.

Upvotes: 2

Related Questions