我&[u8]在二进制缓冲区上有一个切片.我需要解析它,但是我想要使用的很多方法(例如str::find)在切片上似乎不可用.
我已经看到我可以通过使用缓冲切片和我的模式来转换它str,from_utf8_unchecked()但这似乎有点危险(而且也非常hacky).
如何在此切片中找到子序列?我实际上需要模式的索引,而不仅仅是部件的切片视图,所以我认为split不会起作用.
以下内容仅用作示例,而不是有效的Rust代码.
struct Vec<T: Sized, Count> {
a: [T; Count]
}
Run Code Online (Sandbox Code Playgroud)
在C++模板中可能有类似的东西,但我还没有在Rust中看到它.
一次迭代多个变量,重叠(slice::windows)或不重复()可能很有用slice::chunks.
这仅适用于切片; 是否可以为迭代器执行此操作,为方便起见使用元组?
可以写下以下内容:
for (prev, next) in some_iter.windows(2) {
...
}
Run Code Online (Sandbox Code Playgroud)
如果没有,它是否可以作为现有迭代器的特征实现?
我有一个,&[u8]并希望把它变成一个&[u8; 3]没有复制.它应该引用原始数组.我怎样才能做到这一点?
我正在尝试将我的 struct 转换为 a HashMap,但是在 impl 块中时我无法这样做。由于 crate约束,我只能&self用作resolve函数的参数。
use std::collections::HashMap;
pub enum Value {
Int(i64),
Object(HashMap<String, Value>),
}
pub struct WeatherSettings {
forecast_days: i64,
}
impl WeatherSettings {
fn resolve(&self) -> Value {
let json_object: HashMap<String, Value> = *self.into();
Value::Object(json_object)
}
}
impl From<WeatherSettings> for HashMap<String, Value> {
fn from(weather: WeatherSettings) -> HashMap<String, Value> {
let mut json_object = HashMap::new();
json_object.insert("forecast_days".to_owned(),
Value::Int(weather.forecast_days));
return json_object;
}
}
fn main() {}
Run Code Online (Sandbox Code Playgroud)
更直接,我得到错误:
use std::collections::HashMap;
pub …Run Code Online (Sandbox Code Playgroud) 我在 kotlin 中有以下代码,我试图找到 rust 等效项,但不理解 rust 中要转换的链接机制。
val windowSize = 2
val result = listOf(1, 2, 3, 4, 5, 6)
.windowed(windowSize, 1) ; [[1,2], [2,3], [3,4], [4,5], [5,6]]
.map { it.sum() } ; [ 3, 5, 7, 9, 11]
.windowed(2, 1) ; [[3,5], [5,7], [7,9], [9,11] ]
.count { it[0] < it[1] } ; 4
;; result = 4, as there are 4 sequences that have first number less than 2nd,
;; when considering a sliding window over the original …Run Code Online (Sandbox Code Playgroud)