标签: rust-cargo

使用 Cargo 获取执行时间

我对 Rust 很陌生,我想为我的程序的执行计时。我在网上搜索,但到目前为止一无所获。运行后cargo build,我的代码是这样执行的:

bling@bling ~/github/rust/hello_world [master *]
± % ./target/debug/hello_world
Run Code Online (Sandbox Code Playgroud)

Cargo 是否有内置的方式来计时执行还是我需要使用命令行?

time command-line rust rust-cargo

0
推荐指数
2
解决办法
1623
查看次数

检测新结构初始化

我主要来自 OOP 语言,因此让这个概念在 Rust 中工作似乎有点困难。我想实现一个基本计数器,用于记录我创建的该类型的“实例”数量,并将它们保存在向量中以供以后使用。

我尝试了很多不同的事情,首先是创建一个静态向量变量,但是由于它不允许具有析构函数的静态内容,所以这是无法完成的。

这是我的第一次尝试:

struct Entity {
    name: String,
}

struct EntityCounter {
    count: i64,
}

impl Entity {
    pub fn init() {
        let counter = EntityCounter { count: 0 };
    }

    pub fn new(name: String) {
        println!("Entity named {} was made.", name);
        counter += 1; // counter variable unaccessable (is there a way to make it global to the struct (?..idek))
    }
}

fn main() {
    Entity::init();
    Entity::new("Hello".to_string());
}
Run Code Online (Sandbox Code Playgroud)

第二:

struct Entity {
    name: String,
    counter: …
Run Code Online (Sandbox Code Playgroud)

struct rust rust-cargo

0
推荐指数
1
解决办法
545
查看次数

构建trpl-ebook时遇到错误:没有实体的方法中不允许使用模式

我想为我的Kindle 生成Rust编程语言的epub电子书.

我下载了这个Github项目,重建了第二版草案.当我这样做时cargo run --release,我遇到以下错误:

nabarun@pal:~/codesl/trpl-ebook (git:master) $ cargo run --release
   Compiling rustc-serialize v0.3.19
   Compiling memchr v0.1.11
   Compiling kernel32-sys v0.2.2
error[E0642]: patterns aren't allowed in methods without bodies
   --> /home/nabarun/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.19/src/serialize.rs:147:45
    |
147 |                                             &f_name: &str,
    |                                             ^^^^^^^

   Compiling aho-corasick v0.5.3
   Compiling thread-id v2.0.0
   Compiling thread_local v0.2.7
   Compiling regex v0.1.77
error: aborting due to previous error

error: Could not compile `rustc-serialize`.
warning: build failed, waiting for other jobs to finish...
error: build failed
Run Code Online (Sandbox Code Playgroud)

我试图搜索并调试错误但由于我目前对Rust缺乏了解而无法这样做.有没有人遇到这个错误?

我使用的是Ubuntu …

rust rust-cargo

0
推荐指数
1
解决办法
286
查看次数

命名包含字符串".rs"的包装箱有问题吗?

例如,在命名依赖项时可能存在任何未来问题.

[dependencies]
gccjit.rs = { git = "https://github.com/swgillespie/gccjit.rs.git" }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我使用.rs的不是Rust源代码文件.这是不理想的还是没关系,因为它更容易默认为与存储库相同的名称?

如果一个对象被命名,.rs它可能会被自动识别为Rust源代码,但在这种情况下它不是.

rust rust-crates rust-cargo

0
推荐指数
1
解决办法
95
查看次数

我无法从Ubuntu交叉编译到Windows

我想从Ubuntu到Windows交叉编译一些Rust代码,并收到有关onexitbegin的错误。

尝试遵循各种建议,但是它们没有我的特定错误消息:crt2.o:crtexe.c:(.rdata $ .refptr .__ onexitend [.refptr .__ onexitend] + 0x0):对`__onexitend'collect2的未定义引用:错误:ld返回1退出状态

cargo build --release --target x86_64-pc-windows-gnu
Run Code Online (Sandbox Code Playgroud)

期望建造一些东西,但它会炸毁。输出显示以下内容:/ usr / bin / x86_64-w64-mingw32-ld:/home/vince/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/ lib / crt2.o:crtexe.c :(。rdata $ .refptr .__ onexitbegin [.refptr .__ onexitbegin] + 0x0):对__onexitbegin' /usr/bin/x86_64-w64-mingw32-ld: /home/vince/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib/crt2.o:crtexe.c:(.rdata$.refptr.__onexitend[.refptr.__onexitend]+0x0): undefined reference to__onexitend的未定义引用

compiler-errors rust rust-cargo

0
推荐指数
1
解决办法
226
查看次数

如何从`src`目录之外的目录导入模块?

我在学习如何访问模块时陷入困境。我正在尝试插入除 之外的文件srcsrc。它不起作用并且给了我一个错误。这是我的项目树。

$ Project1
.
|-- src
|       |-- main.rs
|   |--FolderinSrcFolder 
|       |--folderinsrcmodule.rs    
|
|--anothersrc
|   |--mod.rs
|
|-- rootmodule.rs
|-- Cargo.toml
|-- Cargo.lock
Run Code Online (Sandbox Code Playgroud)

我怎样才能访问anothersrc/mod.rs src/main.rs?我如何rootmodule.rs访问src/main.rs

我已经阅读了 Rust 文档。

module rust rust-cargo

0
推荐指数
1
解决办法
2528
查看次数

如何制作一个可以在不使用 `cargo run` 的情况下执行的 Rust 程序?

如何在 Rust 中创建一个程序cargo run,只需单击文件即可在任何地方执行而无需使用?

有箱子吗?我已经为蛇游戏编写了代码,我想通过点击一个文件来运行它。

rust rust-cargo

0
推荐指数
2
解决办法
2602
查看次数

无法使用curl 在 RHEL 6.10 上安装 Rust

我尝试使用curl 在 RHEL 6.10 上安装 rust 1.48 并收到以下错误

info: downloading installer
Cannot execute /tmp/tmp.ShsTMGuMqK/rustup-init (likely because of mounting /tmp as noexec).
Please copy the file to a location where you can execute binaries and run ./rustup-init.
Run Code Online (Sandbox Code Playgroud)

这是我安装 Rust 的命令

curl --insecure -sSf -o $HOME/rust/rustup-init https://sh.rustup.rs
chmod +x $HOME/rust/rustup-init
./$HOME/rust/rustup-init -s -y --default-toolchain 1.48
Run Code Online (Sandbox Code Playgroud)

我发现这个https://github.com/rust-lang/rust/issues/39771但它不起作用。我的 $HOME 目录也有写权限

rust rust-cargo

0
推荐指数
1
解决办法
1212
查看次数

离线时如何使用cargo进行安装?

我想在我的工作区域使用 bat 和 lsd 。

但为了安全起见,我公司的 Centos 6 Linux 服务器已与互联网断开连接。

但我们可以安装一些有助于工作的东西。

> cargo install --locked bat
    Updating crates.io index
warning: spurious network error (2 tries remaining): failed to resolve address for github.com: Temporary failure in name resolution; class=Net (12)
warning: spurious network error (1 tries remaining): failed to resolve address for github.com: Temporary failure in name resolution; class=Net (12)
error: failed to fetch `https://github.com/rust-lang/crates.io-index`

Caused by:
  failed to resolve address for github.com: Temporary failure in name resolution; class=Net (12)
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

installation rust rust-cargo

0
推荐指数
1
解决办法
3313
查看次数

`cargo install` 是否下载在其他人的计算机上编译的二进制文件?

是否cargo install下载在别人计算机上编译的二进制文件?执行时有时会下载此类预建文件吗cargo install?的输出cargo install表明编译已经发生,但我不确定我是否可以相信它cargo install永远不会将任何预编译的内容下载到我的计算机上。因此,只要我可以,我就可以手动克隆存储库并自己编译二进制文件,.eg

git clone https://github.com/mitnk/cicada.git && cd cicada && cargo build --release && sudo mv target/release/cicada /usr/local/bin 
Run Code Online (Sandbox Code Playgroud)

而不是安装,例如cargo install -f cicada。我只做前者,因为我想避免下载在别人的电脑上编译的二进制文件?另一个原因是我更喜欢用--release. 我不太确定cargo install执行时是否会发生这种优化。

rust rust-cargo

0
推荐指数
1
解决办法
575
查看次数