【问题标题】:How do I compile the rust GTK example to the gtk crate? "#[doc(alias)] is experimental"如何将 rust GTK 示例编译到 gtk crate? “#[doc(alias)] 是实验性的”
【发布时间】:2022-01-25 03:56:26
【问题描述】:

我一直在努力学习 Rust 语言。我已经设法创建了一些简单的命令行应用程序,但现在我要继续看看是否可以创建一个使用图形界面的应用程序。我研究了几个 GUI 库,包括 iced 和 Qt API,但都没有成功。我最近登陆 GTK。我从 gtk crate 文档中获取了示例

https://gtk-rs.org/gtk3-rs/stable/latest/docs/gtk/

该页面上没有关于将什么放入 Cargo.toml 文件的信息,所以我一直在尝试弄清楚。到目前为止,我在网上找到的答案根本没有帮助。于是我去了 crates.io 并获取了最新版本的 gtk crate,0.14.3,并将其放入 toml 中。

[package]
name = "gtkhello"
version = "0.1.0"
authors = ["brian"]
edition = "2018"

[dependencies]
gtk = "0.14.3"

这是示例程序,原封不动地从文档中复制。

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow};

fn main() {
    let app = Application::builder()
        .application_id("org.example.HelloWorld")
        .build();

    app.connect_activate(|app| {
        // We create the main window.
        let win = ApplicationWindow::builder()
            .application(app)
            .default_width(320)
            .default_height(200)
            .title("Hello, World!")
            .build();

        // Don't forget to make all widgets visible.
        win.show_all();
    });

    app.run();
}

当我尝试使用 cargo 构建它时,库的编译失败。如果库本身无法编译,我不知道如何继续。以下是错误:

[brian@localhost gtkhello]$ cargo build
   Compiling glib-macros v0.14.1
   Compiling gdk-pixbuf-sys v0.14.0
error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:202:1
    |
202 | #[doc(alias = "get_full_ident")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:248:1
    |
248 | #[doc(alias = "get_keyword")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:366:1
    |
366 | #[doc(alias = "get_expr")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:413:1
    |
413 | #[doc(alias = "get_return_kind")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:474:5
    |
474 |     #[doc(alias = "get_closure")]
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

error[E0658]: `#[doc(alias)]` is experimental
   --> /home/brian/.cargo/registry/src/github.com-1ecc6299db9ec823/glib-macros-0.14.1/src/clone.rs:579:1
    |
579 | #[doc(alias = "get_closure")]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: see issue #50146 <https://github.com/rust-lang/rust/issues/50146> for more information

   Compiling gdk-sys v0.14.0
error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0658`.
error: could not compile `glib-macros`.

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed

我访问了issue 的错误消息中建议的链接,它的结尾是:

bors 于 2020 年 9 月 14 日在 57c5f40 中关闭了这个

所以我想我的问题有几个方面。我应该在 Cargo.toml 文件中添加什么来使这个示例正常工作?我应该如何从这个简单的例子中分支出来,将来如何找到这些信息?如何绕过无法编译的必需板条箱?我如何告诉 cargo 我可以使用实验代码?

最大的问题是“我错过了什么?”

【问题讨论】:

  • 你的 Rust 版本是什么?看起来它太旧了,因此不受 gtk-rs 的支持。
  • $ rustc --version rustc 1.43.1

标签: rust gtk


【解决方案1】:

引用的功能以前不稳定,但稳定了in version 1.48.0。因此,gtk-rs 支持的最低 Rust 版本至少为 1.48.0。

一般来说,如果您看到某些 crate 编译失败并引用“不稳定的特性”,但相应的跟踪问题已经关闭,这通常意味着您的编译器版本比相关 crate 支持的最低版本旧。如果跟踪问题是开放的,即功能尚未稳定,则 crate 可能仅适用于每晚 - 在这种情况下,您必须使用 rustup install nightly 添加必要的工具链,然后通过 rustup default nightly 将任一设置设置为全局默认值,或仅为当前项目设置override(最简单的方法是使用rustup override nightly)。

【讨论】:

  • 感谢您提供的信息。我会考虑升级我的编译器。我正在运行 linux 并从存储库中安装了 rust,所以我必须做一些研究来弄清楚如何升级。
  • 建议使用 Rustup - rustup.rs - 代替。系统存储库中的版本可能很旧,具体取决于维护者。
  • 我卸载了 rust 的存储库版本并使用 rustup 重新安装。我的 rustc 版本现在是 1.57.0。该示例编译干净并且工作正常。感谢您的帮助。
猜你喜欢
  • 2021-09-26
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多