use std::collections::HashSet;
fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 {
for needle in needles {
let mut needle_custom;
if needle_type == "double" {
needle_custom = needle.to_string() + &needle.to_string();
} else {
needle_custom = needle.to_string() + &needle.to_string() + &needle.to_string();
}
if input.contains(needle_custom) {
println!("found needle {:?}", needle_custom);
}
}
return 1;
}
Run Code Online (Sandbox Code Playgroud)
use std::collections::HashSet;
fn character_count(needles: &HashSet<char>, needle_type: &str, input: &str) -> i32 {
for needle in needles {
let mut needle_custom;
if needle_type == "double" {
needle_custom = …Run Code Online (Sandbox Code Playgroud) 我有一个JSON数据流,其中某些JSON对象可能缺少某些字段或具有我事先不知道的字段。
我的解决方案是使用:
let v: Value = serde_json::from_str(data)?;
Run Code Online (Sandbox Code Playgroud)
如何处理该字段stuff?如果我知道它存在,则可以使用:
v["stuff"]
Run Code Online (Sandbox Code Playgroud)
如果数据stuff中没有字段,该如何处理?
I'm trying to learn how to do locks in Rust the way they work in Go. With Go I can do something like:
type Info struct {
sync.RWMutex
height uint64
verify bool
}
Run Code Online (Sandbox Code Playgroud)
If I have some function/method acting on info I can do this:
func (i *Info) DoStuff(myType Data) error {
i.Lock()
//do my stuff
}
Run Code Online (Sandbox Code Playgroud)
It seems like what I need is the sync.RWMutex, so this is what I have tried:
pub struct Info {
pub lock: sync.RWMutex, …Run Code Online (Sandbox Code Playgroud)