小编wha*_*0xx的帖子

为什么 Rust 的 std::iter::zip 似乎具有内部可变性?

据我了解,我不应该能够对不可变的数据进行可变引用。因此,如果我想创建一些基于改变内部状态struct的实现Iterator,那么我必须将其任何实例声明struct为可变的,以便能够创建所需的可变引用。

为了进行演示,我构建了一个简单的示例,如下所示:

struct MyIter {
    internal_state: u32
}

impl MyIter {
    fn new() -> MyIter {
        MyIter{ internal_state: 0 }
    }
}

impl Iterator for MyIter {
    type Item = u32;
    fn next(&mut self) -> Option<u32> {
        self.internal_state += 1;
        Some(self.internal_state)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::iter::zip;

    #[test]
    fn mutable_declaration_no_zip() {
        let mut my_iter = MyIter::new();
        assert_eq!(my_iter.next(), Some(1));
        assert_eq!(my_iter.next(), Some(2));
        assert_eq!(my_iter.next(), Some(3));
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以正常编译并通过,而如果我将测试函数更改为具有不可变的声明my_iter,如下所示,则它拒绝使用预期消息进行编译error[E0596]: cannot borrow …

rust

6
推荐指数
1
解决办法
174
查看次数

为什么基于 Alpine Linux 的 Docker 镜像无法找到在绝对路径下执行的命令?

我是 Docker 新手。我想容器化一个小环境,以便可以运行可执行文件,但我陷入困境,因为我什至无法运行可执行文件。

我的文件夹结构如下所示:

example/
|-Dockerfile
|-hello_world
Run Code Online (Sandbox Code Playgroud)

我的Dockerfile看起来像这样:

# Use Alpine Linux as the base image
FROM alpine:latest

# Set the working directory inside the container
WORKDIR /app

# Copy the executable to the container
COPY hello_world /app/

# Set the permissions for the executable
RUN chmod +x /app/hello_world

# Define the command to run your server when the container starts
ENTRYPOINT ["/app/hello_world"]
Run Code Online (Sandbox Code Playgroud)

然后我运行:

> sudo docker build -t example .

> sudo docker run --name example_container example …

unix rust docker dockerfile alpine-linux

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

标签 统计

rust ×2

alpine-linux ×1

docker ×1

dockerfile ×1

unix ×1