gpt4 book ai didi

Rust GTK+ 示例无法编译,因为 Option<&str> : From> is not satisfied

转载 作者:行者123 更新时间:2023-11-29 08:03:00 26 4
gpt4 key购买 nike

Rust GTK examples , 有一个叫做 notebook .它不编译:

for i in 1..4 {
let title = format!("sheet {}", i);
let label = gtk::Label::new(Some(&title));
//let label = gtk::Label::new(Some("test1111")); # works well
notebook.create_tab(&title, label.upcast());
}

错误是:

the trait bound `std::option::Option<&str>: std::convert::From<std::option::Option<&std::string::String>>` is not satisfied

它是关于什么的以及如何解决它?

最佳答案

您似乎正在使用 gtk-rs/examples 的旧副本.在current master , notebook.rs 中的循环看起来像这样:

let mut notebook = Notebook::new();

for i in 1..4 {
let title = format!("sheet {}", i);
let label = gtk::Label::new(&*title);
notebook.create_tab(&title, label.upcast());
}

这段代码编译 - 不同之处在于它使用了 &*title转换 String变成可转换为 Option<&str> 的东西.

PR#447 , gtk-rs从使用 Option<&str> 切换至 Into<Option<&str>>在接受可选字符串的函数的参数中,包括 gtk::Label::new .这使 API 更符合人体工程学,因为常量字符串参数可以写成 "string"而不是 Some("string") .但是,此更改并非 100% 向后兼容 - Into<Option<...>>模式不支持传递 Some(&string)string被拥有String - 必须写 &*string或更明确的 string.as_str()相反。

关于Rust GTK+ 示例无法编译,因为 Option<&str> : From<Option<&String>> is not satisfied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com