小编Fre*_*ors的帖子

如果我针对大小 (z) 而不是速度 (3) 进行优化,我会改变什么速度增益?

我读了两个:

我不明白的是,如果我使用,我会改变哪种速度增益:

[profile.release]
opt-level = "z"
Run Code Online (Sandbox Code Playgroud)

代替:

[profile.release]
opt-level = 3
Run Code Online (Sandbox Code Playgroud)
  • 今天opt-level = 3opt-level运行速度的最佳设置(对于部分)是否正确?

  • 如果我改为使用,opt-level = "z"我会降低运行时性能,对吗?

我对构建/编译速度不感兴趣。

optimization compilation llvm rust rust-cargo

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

我可以在 VS Code 中设置“文件 > 退出”键盘快捷键吗?

在我的日常工作中,我经常会在不同的 VS Code 实例中打开许多项目。

我经常发现自己放弃使用鼠标和“文件>退出”,以便在明天重新打开它时立即恢复所有窗口。

我找不到为“文件>退出”添加键盘快捷键的方法。这是可能的?

还有其他避免鼠标移动的建议吗?

keyboard keyboard-shortcuts visual-studio-code

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

如何更新这个 Svelte 商店而不每次都重新创建它?

这里是 REPL:https://svelte.dev/repl/56770fec88af4b76bdc8ea962178854e ?version=3.42.1

这里是代码:

应用程序.svelte:

<script>
    import {editableStore} from "./store";
    
    let name = "John"

    $: player = editableStore(name);
</script>

<h1>Hello {$player.name}!</h1>

<button on:click={() => name = (name === "Bob" ? "Jerry" : "Bob")}>
    Change name
</button>

<h2>Log:</h2>

{#each $player.log as log}
    <li>{log}</li>
{/each}
Run Code Online (Sandbox Code Playgroud)

商店.js:

import {writable} from "svelte/store";

const defaultStore = {
    name: "Bob",
    age: 18,
    log: []
};

export const editableStore = (name) => {
    console.log("Recreated with name:", name);

    const {subscribe, update} = writable({...defaultStore}, () => () => …
Run Code Online (Sandbox Code Playgroud)

javascript svelte svelte-component svelte-store svelte-3

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

为什么会出现错误“特性`FnOnce&lt;()&gt;`已实现,但该实现不是`const`”?

我是 Rust 新手。我正在使用 Rust 1.60.0。

我正在尝试使用此代码(PLAYGROUND HERE):

pub const VERSION: &'static str = option_env!("VERSION").unwrap_or_else(|| "dev");
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

the trait bound `[closure@src\constants.rs:4:81: 4:89]: ~const FnOnce<()>` is not satisfied
the trait `~const FnOnce<()>` is not implemented for `[closure@src\constants.rs:4:81: 4:89]`
wrap the `[closure@src\constants.rs:4:81: 4:89]` in a closure with no arguments: `|| { /* code */ }`rustcE0277
constants.rs(4, 66): the trait `FnOnce<()>` is implemented for `[closure@src\constants.rs:4:81: 4:89]`, but that implementation is not `const`
option.rs(797, 12): required by a bound in `Option::<T>::unwrap_or_else`
Run Code Online (Sandbox Code Playgroud)

你能帮我理解它出了什么问题吗?

rust

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

如何在枚举中使用字符串值并避免“预期的`isize`,找到的`&amp;str`”?

我正在尝试使用,enum但我需要将字符串值保存在数据库、Redis 等中。

pub enum Place {
  Square = "1",
  House = "2",
  Office = "3",
  // and so on... Garage = "4",
}
Run Code Online (Sandbox Code Playgroud)

但编译器抛出:

error[E0308]: mismatched types
  |
3 |     Square = "1",
  |             ^^^ expected `isize`, found `&str`

error[E0308]: mismatched types
  |
4 |     House = "2",
  |            ^^^ expected `isize`, found `&str`

error[E0308]: mismatched types
  |
5 |     Office = "3",
  |            ^^^ expected `isize`, found `&str`

For more information about this error, try `rustc --explain …
Run Code Online (Sandbox Code Playgroud)

rust

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

为什么 strconv.ParseUint 比 strconv.Atoi 慢?

我正在对从stringtointuint使用以下代码的解组进行基准测试:

package main

import (
    "strconv"
    "testing"
)

func BenchmarkUnmarshalInt(b *testing.B) {
    for i := 0; i < b.N; i++ {
        UnmarshalInt("123456")
    }
}

func BenchmarkUnmarshalUint(b *testing.B) {
    for i := 0; i < b.N; i++ {
        UnmarshalUint("123456")
    }
}

func UnmarshalInt(v string) int {
    i, _ := strconv.Atoi(v)
    return i
}

func UnmarshalUint(v string) uint {
    i, _ := strconv.ParseUint(v, 10, 64)
    return uint(i)
}
Run Code Online (Sandbox Code Playgroud)

结果:

Running tool: C:\Go\bin\go.exe test -benchmem -run=^$ myBench/main …
Run Code Online (Sandbox Code Playgroud)

performance benchmarking type-conversion go microbenchmark

0
推荐指数
1
解决办法
224
查看次数

我应该使用 Get 方法来获取值还是应该直接使用字段?

我第一次在 Go 中使用 protobuf(和 protoc)。

message MyProtoStruct {
  string description = 1;
}
Run Code Online (Sandbox Code Playgroud)

我有点困惑:

  1. 我应该使用方法来获取值(如MyProtoStruct.GetDescription())还是

  2. 我应该直接使用字段(如MyProtoStruct.Description)吗?

go protocol-buffers proto protoc

0
推荐指数
1
解决办法
137
查看次数

如果我从方法中得到“None”,是否有提前返回的方法?

None如果我从方法中获取 a ,是否有提前返回的方法?例子:

pub async fn found_player(id: &str) -> Result<Option<Player>> {
    let player = repo // player here is Option<Player>
        .player_by_id(id)
        .await?; // I would like to use here a magic method to return here immediately if is None with `Ok(None)`
  
    if player.is_none() {
        return Ok(None);
    }

    // Do some stuff here but WITHOUT using player.unwrap(). I would like to have it already unwrapped since is not None

    Ok(Some(player))
}
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的事情Ok_or(),但我认为它们现在就是我所需要的。我能怎么做?

我不想使用matchorif else因为我需要尽可能少一些冗长。

rust

0
推荐指数
1
解决办法
463
查看次数

为什么 Rust 要求 .collect() 提供类型注释?

鉴于下面的代码足够明确,我想为什么 Rust 要求类型注释?

铁锈游乐场

pub struct Score {
    pub id: Option<String>,
}

fn main() {
    let rows = vec![
        Score{
            id: None,
        },
        Score{
            id: Some("test".to_string()),
        },
    ];
    
    let existing_scores = rows
        .iter()
        .map(|o| o.id.unwrap_or_default())
        .collect();
        
    dbg!(existing_scores);
}
Run Code Online (Sandbox Code Playgroud)

rust

0
推荐指数
1
解决办法
103
查看次数

有没有办法在多个处理程序中使用相同的 request.Body 而无需手动编写大量代码,或者我需要改变我这样做的方式?

这篇很棒的文章在这里:https : //www.alexedwards.net/blog/how-to-properly-parse-a-json-request-body 很好地解释了如何编写 Golang 处理程序。

我需要使用两个处理程序,一个接一个,仅当第一个出现错误时。

像这样:

func main() {
    r := chi.NewRouter()

    r.Post("/api", MyHandlers)
}

func MyHandlers(w http.ResponseWriter, r *http.Request) {

    err := DoSomething(w, r)

    if err != nil {
        println("OMG! Error!")
        DoSomethingWithThisOneInstead(w, r)
    }

}

func DoSomething(w http.ResponseWriter, r *http.Request) error {
    // here I need to read request's Body
    // and I can use io.TeeReader()
    // and I can use all the code in the amazing article example
    // but I don't want to, because …
Run Code Online (Sandbox Code Playgroud)

architecture middleware handler go go-chi

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

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

有没有一种简洁的方法来检查选项是否为“无”或其某些内容是否为空?

还有比下面的代码更简洁的吗?

let player_list: Option<Vec<Player>>;

// more code here

if player_list.is_none() || player_list.as_ref().unwrap().is_empty() {
  // do something here if player_list is none or empty
}
Run Code Online (Sandbox Code Playgroud)

我想避免这一||部分。

rust option-type

-7
推荐指数
1
解决办法
242
查看次数