AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-3526865

Olivier Lasne's questions

Martin Hope
Olivier Lasne
Asked: 2024-12-21 21:33:06 +0800 CST

将 i128 转换为 f64 的正确方法是什么

  • 5

我想将i128转换为f64 。

我知道我可以默默地施展as。

let nb: i128 = 1337;
let converted = nb as f64;

但它没有给我处理任何错误的机会。

有没有办法做类似的事情

let nb: i128 = 1337;
let converted: f64 = nb.try_into()?;

如果转换后的值不适合 f64 ,我可以在哪里处理错误?

rust
  • 1 个回答
  • 53 Views
Martin Hope
Olivier Lasne
Asked: 2024-09-15 03:22:59 +0800 CST

是否可以定义一个变量在算术运算中始终饱和?

  • 7

我有一个变量,我对它进行了很多算术运算。

我的代码是这样的:

let a: u32 = 3;
let res = (a*2) - 42

a当太低时就会溢出,所以我必须使用它saturating_sub()来解决问题:

let a: u32 = 3;
let res = (a * 2).saturating_sub(42);

这确实很有效,但saturating_sub可读性不如-。

由于我有很多这样的操作,而且在每种情况下,我都希望a饱和。我想知道是否可以定义应该a始终饱和。而不是用替换-每个saturate_sub。

rust
  • 1 个回答
  • 78 Views
Martin Hope
Olivier Lasne
Asked: 2024-09-11 17:11:09 +0800 CST

为什么在匹配“u64 % 2”时“2_u32..=u32::MAX”未被覆盖?

  • 10

如果我尝试构建以下代码:

fn main () {
    let my_val: u32 = 42;
    
    match my_val % 2 {
        0 => println!("We're even now"),
        1 => println!("Well, that's odd"),
    }
}

我会收到以下错误信息:

error[E0004]: non-exhaustive patterns: `2_u32..=u32::MAX` not covered
 --> src/main.rs:4:11
  |
4 |     match my_val % 2 {
  |           ^^^^^^^^^^ pattern `2_u32..=u32::MAX` not covered
  |
  = note: the matched value is of type `u32`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
  |
6 ~         1 => println!("Well, that's odd"),
7 ~         2_u32..=u32::MAX => todo!(),
  |

我不太明白。 代表什么情况2_u32..=u32::MAX?

rust
  • 1 个回答
  • 54 Views
Martin Hope
Olivier Lasne
Asked: 2024-09-05 18:53:43 +0800 CST

创建一个可以添加 i64、f64 或 i64 和 f64 混合的 Rust 函数

  • 5

我想编写一个add函数,可以将这两个参数作为i64输入。f64

我得出了以下结论:

use std::ops::Add;

fn add<T: Add<Output = T>>(a: T, b: T) -> T{
    a + b
}

fn main() {
    let a: i64 = 10;
    let b: i64 = 20;
    let c: f64 = 10.5;
    let d: f64 = 20.5;

    println!("{:?}", add(a, b)); // Outputs: 30
    println!("{:?}", add(c, d)); // Outputs: 31.0
}

是否可以修改此功能以便可以实现:

  • 一个参数是i64
  • 另一个参数是f64

如果任一参数是,f64我们进行强制转换并返回f64。

类型a 类型b 返回type
i64 i64 i64
i64 f64 f64
f64 i64 f64
f64 f64 f64

主要函数将有以下输出:

fn main() {
    let a: i64 = 10;
    let b: i64 = 20;
    let c: f64 = 10.5;
    let d: f64 = 20.5;

    println!("{:?}", add(a, b)); // Outputs: 30    | i64 + i64 -> i64
    println!("{:?}", add(a, c)); // Outputs: 20.5  | i64 + f64 -> f64
    println!("{:?}", add(c, a)); // Outputs: 20.5  | f64 + i64 -> f64
    println!("{:?}", add(c, d)); // Outputs: 30.0  | f64 + f64 -> f64
}
generics
  • 1 个回答
  • 39 Views
Martin Hope
Olivier Lasne
Asked: 2024-06-02 15:55:42 +0800 CST

当所有参数都实现复制特征时,为什么这个函数需要生命周期?

  • 5

在下面的代码中,我不明白为什么 Span需要 life。由于所有参数都实现了复制特征,我的理解是 Span 拥有所有必需的变量。

use ratatui::{
    style::{Color, Style},
    text::Span,
};

/// Take a u8, and render a colorized ascii, or placeholdler
fn render_ascii_char(val: u8) -> Span {
    match val {

        val if val > 0x20 && val < 0x7f => {
            Span::styled(
                val.to_string(),
                Style::default().fg(Color::LightCyan)
            )
        },

        _ => {
            Span::styled(
                "•",
                Style::default().fg(Color::Yellow)
            )
        }
    }
}

fn test_function() -> Span {
    let val = 0x42;

    render_ascii_char(val)
}

fn main() {
    let _my_span = test_function();
}

这段代码使用ratatui.波纹管Cargo.toml:

[package]
name = "span_lifetime"
version = "0.1.0"
edition = "2024"

[dependencies]
crossterm = "0.27.0"
ratatui = "0.26.2"

编译时出现以下错误消息cargo build:

$ cargo build
   Compiling span_lifetime v0.1.0 (/tmp/playground/span_lifetime)
error[E0106]: missing lifetime specifier
 --> src/main.rs:7:34
  |
7 | fn render_ascii_char(val: u8) -> Span {
  |                                  ^^^^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
  |
7 | fn render_ascii_char(val: u8) -> Span<'static> {
  |                                      +++++++++

error[E0106]: missing lifetime specifier
  --> src/main.rs:26:23
   |
26 | fn test_function() -> Span {
   |                       ^^^^ expected named lifetime parameter
   |
   = help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime, but this is uncommon unless you're returning a borrowed value from a `const` or a `static`, or if you will only have owned values
   |
26 | fn test_function() -> Span<'static> {
   |                           +++++++++

For more information about this error, try `rustc --explain E0106`.
error: could not compile `span_lifetime` (bin "span_lifetime") due to 2 previous errors

使用<'static>生命周期似乎不是一个好主意,因为我不只将此函数与常量一起使用。

使用引用val: &u8作为参数,而不是val: u8确实有效。但当传递的值超出范围时,就会产生问题。

到目前为止我的解决方案是指定生命周期:

fn render_ascii_char<'a>(val: u8) -> Span<'a> {
    match val {
[...]

但我真的不明白为什么需要这样做,也不明白这一生的含义。

rust
  • 1 个回答
  • 54 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve