小编Jam*_*ith的帖子

当提供的输入是字符串引用时,str::contains 不起作用

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)

string rust

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

如何使用serde_json和Value枚举处理可能丢失的字段?

我有一个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中没有字段,该如何处理?

rust serde

1
推荐指数
2
解决办法
112
查看次数

How to lock a Rust struct the way a struct is locked in Go?

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)

mutex locking rust

-3
推荐指数
1
解决办法
104
查看次数

标签 统计

rust ×3

locking ×1

mutex ×1

serde ×1

string ×1