小编ill*_*zur的帖子

迭代结构中的Vec - 无法摆脱借来的内容

我正在为一个结构编写一个函数,该结构包含Vec我尝试迭代的地方Vec:

struct Object {
    pub v: Vec<f32>,
}

impl Object {
    pub fn sum(&self) -> f32 {
        let mut sum = 0.0;
        for e in self.v {
            sum += e;
        }
        sum
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:8:18
  |
8 |         for e in self.v {
  |                  ^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)

我的理解是,因为self借用了并且for循环迭代试图将vout 的元素移动到e.

从错误代码中,我读到一个潜在的解决方案是取得所有权,但我不太确定如何做到这一点.

我不是要修改向量或其元素.我只是想使用这些元素来运行一些计算.

rust

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

clang-format覆盖WebKit样式的多行注释

我正在尝试使用clang-format来清理我的存储库中的代码.我们使用WebKit样式作为格式化的基础,但是我们还希望确保多行注释的格式正确.

根据我的理解,可以通过定义.clang格式文件来覆盖给定样式的格式规则:

BasedOnStyle: WebKit
AlignTrailingComments: true
Run Code Online (Sandbox Code Playgroud)

这种方式clang-format应该对齐尾随注释.

给定输入文件:

    /**
     * This is a multi-line comment
     */
    void function() {
        /**
         * This is comment inside the function
         */
    }
Run Code Online (Sandbox Code Playgroud)

我的期望是以下输出

/**
 * This is a multi-line comment
 */
void function()
{
    /**
     * This is comment inside the function
     */
}
Run Code Online (Sandbox Code Playgroud)

但是我得到的是:

/**
     * This is a multi-line comment
     */
void function()
{
    /**
         * This is comment inside the function
         */
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试将Webkit的格式化选项转储为.clang格式文件,并将AlignTrailingComments从false更改为true.这也没有任何影响.

Webkit样式中是否存在一些干扰AlignTrailingComments选项的选项?

c++ clang-format

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

标签 统计

c++ ×1

clang-format ×1

rust ×1