我已经编写了一些测试,我需要声明两个数组相等.有些阵列[u8; 48]而有些则是[u8; 188]:
#[test]
fn mul() {
let mut t1: [u8; 48] = [0; 48];
let t2: [u8; 48] = [0; 48];
// some computation goes here.
assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
}
Run Code Online (Sandbox Code Playgroud)
我在这里收到多个错误:
error[E0369]: binary operation `==` cannot be applied to type `[u8; 48]`
--> src/main.rs:8:5
|
8 | assert_eq!(t1, t2, "\nExpected\n{:?}\nfound\n{:?}", t2, t1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `[u8; 48]`
= note: this error originates in …Run Code Online (Sandbox Code Playgroud) 我在里面指定了一些功能Cargo.toml:
[features]
complex = []
simple = []
Run Code Online (Sandbox Code Playgroud)
当我构建项目时,我使用cargo build --features="complex"或simple.
在某些函数中,我想根据使用的功能返回一个值:
fn test() -> u32 {
let x: u32 = 3;
if cfg!(feature = "complex") {
let y: u32 = 2;
x + y
}
if cfg!(feature = "simple") {
let y: u32 = 1;
x + y
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为它试图评估两个表达式。在我的情况下使用cfg!宏的正确方法是什么?
我有一个从 API 获取的数据,如下所示(当然是 JSON 形式):
0,1500843600,8872
1,1500807600,18890
2,1500811200,2902
.
.
.
Run Code Online (Sandbox Code Playgroud)
其中第二列是以刻度为单位的日期/时间,第三列是某个值。我基本上拥有几个月内每天每小时的数据。现在,我想要实现的是我想获得每周第三列的最小值。我有一个代码段,如下所示:
from bs4 import BeautifulSoup
import datetime
import json
import pandas
# Partially removed for brevity.
# dic holds now the data that I get from the API.
dic = json.loads(soup.prettify())
df = pandas.DataFrame(columns=['Timestamp', 'Value'])
for i in range(len(dic)):
df.loc[i] = [datetime.datetime.fromtimestamp(int(dic[i][1])).strftime('%d-%m-%Y %H:%M:%S'), dic[i][2]]
df.sort_values(by=['Timestamp'])
df['Timestamp'] = pandas.to_datetime(df['Timestamp'])
df.set_index(df['Timestamp'], inplace=True)
print(df['Value'].resample('W').min())
Run Code Online (Sandbox Code Playgroud)
虽然,这并没有给我完全正确的结果,但也有一些结果是NaN。此外,我还想获取时间戳和最小值,这样我就知道最小值发生在一周中的哪个日期/时间。有什么想法可以实现我想要的吗?
在Go中,有一些make和append函数,第一个让你创建一个指定类型,长度和容量的切片,而第二个让你将一个元素附加到指定的切片.它或多或少与这个玩具示例相似:
func main() {
// Creates a slice of type int, which has length 0 (so it is empty), and has capacity 5.
s := make([]int, 0, 5)
// Appends the integer 0 to the slice.
s = append(s, 0)
// Appends the integer 1 to the slice.
s = append(s, 1)
// Appends the integers 2, 3, and 4 to the slice.
s = append(s, 2, 3, 4)
}
Run Code Online (Sandbox Code Playgroud)
Rust是否提供与切片一起使用的类似功能?
我正在重写Rust中的C代码,它严重依赖u32变量并将它们包装起来.例如,我有一个像这样定义的循环:
#define NWORDS 24
#define ZERO_WORDS 11
int main()
{
unsigned int i, j;
for (i = 0; i < NWORDS; i++) {
for (j = 0; j < i; j++) {
if (j < (i-ZERO_WORDS+1)) {
}
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,该if语句需要u32最初包含几个值i = 0.我遇到了这个wrapping_neg方法,但它似乎只是计算-self.是否还有更灵活的方式u32在Rust中使用包装?