我执行FromIterator了[MyStruct;4]其中MyStruct的一个小副本结构.我目前的实施是
fn from_iter<I: IntoIterator<Item=MyStruct>>(iter: I) -> Self {
let mut retval = [Default::default();4];
for (ret, src) in retval.iter_mut().zip(iter) {
*ret = src;
}
retval
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但我不确定for循环是否像惯用的那样.是否有类似的方法Slice::fill(iter)可以更干净地(也许更有效)实现这一目标?
我使用的是 RuboCop 0.46.0 和 Ruby 2.3.1。
Style/FrozenStringLiteralComment:
EnforcedStyle: always
Run Code Online (Sandbox Code Playgroud)
# frozen_string_literal: true
MY_CONSTANT = 'mystring'
Run Code Online (Sandbox Code Playgroud)
运行时,rubocop -D返回:
Inspecting 1 file
C
Offenses:
constant.rb:3:15: C: Style/MutableConstant: Freeze mutable objects assigned to constants.
MY_CONSTANT = 'mystring'
^^^^^^^^^^
1 file inspected, 1 offense detected
Run Code Online (Sandbox Code Playgroud)
有没有理由 MutableConstant 没有观察我的冻结字符串文字注释?