小编cha*_*pok的帖子

如何格式化具有特定精度的f32和前置零?

我应该在println!宏中使用什么格式字符串才能打印0.000000.000

println!("={:05.3}", 0.0);
Run Code Online (Sandbox Code Playgroud)

输出: =0.000

预期: =00000.000

floating-point formatting rust

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

为什么有时可能从不可变数组中借用&mut引用?

让我们尝试编译这段代码:

trait Bar {
    fn bar(&mut self);
}

fn foo(a1: &mut Bar, j: usize) {
    let a = [a1];
    a[0].bar(); //compilation ok
    a[j % 2].bar();
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error[E0596]: cannot borrow immutable local variable `a` as mutable
 --> src/main.rs:8:5
  |
6 |     let a = [a1];
  |         - consider changing this to `mut a`
7 |     a[0].bar(); //compilation ok
8 |     a[j % 2].bar();
  |     ^ cannot borrow mutably
Run Code Online (Sandbox Code Playgroud)

为什么a[0].bar()好,但a[j % 2].bar()失败了?这是编译器错误吗?

rust

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

为什么指针算术是段错误的原因?

为什么这个指针算术(没有读取或写入这些指针后面的数据)是segfault的原因?

#![allow(dead_code,unused_variables)]
use std::cell::Cell;

struct Bar<T: ?Sized> {
    a: Cell<usize>,
    value: T,
}

unsafe fn foo<T: ?Sized>(v: &T) {
    let fake: &Bar<T> = std::mem::zeroed();

    // segfault on this line
    // we are not reading or writing uninitialized data behind the reference, 
    // but only doing pointer arithmetic. We are not reading or writing 
    // uninitialized vtable, but only copy the vtable pointer.
    let fake_val = &fake.value;
}


fn main() {
    use std::any::Any;

    let some_ref: &Any = &42 as &Any;
    unsafe …
Run Code Online (Sandbox Code Playgroud)

rust

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

为什么我不能在索引到不可变的Vec <RefCell>之后调用borrow_mut()?

让我们尝试编译这段代码:

use std::cell::RefCell;

struct Foo {
    v: Vec<RefCell<u8>>,
}

impl Foo {
    fn f(&self, i: usize) {
        let t = &mut *self.v[i].borrow_mut();
        //let t = &mut *{self.v[i].borrow_mut()}; //compiled ok
    }
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

编译错误:

error[E0596]: cannot borrow field `self.v` of immutable binding as mutable
 --> src/main.rs:9:23
  |
8 |     fn f(&self, i: usize) {
  |          ----- use `&mut self` here to make mutable
9 |         let t = &mut *self.v[i].borrow_mut();
  |                       ^^^^^^ cannot mutably borrow field of immutable binding …
Run Code Online (Sandbox Code Playgroud)

rust

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

为什么通过DerefMut可变地借用闭包是行不通的?

我试图可变地借用一个可变变量。Deref并且DerefMut已针对实现Foo,但编译失败:

use std::ops::{Deref, DerefMut};

struct Foo;

impl Deref for Foo {
    type Target = FnMut() + 'static;
    fn deref(&self) -> &Self::Target {
        unimplemented!()
    }
}

impl DerefMut for Foo {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unimplemented!()
    }
}

fn main() {
    let mut t = Foo;
    t();
}
Run Code Online (Sandbox Code Playgroud)
use std::ops::{Deref, DerefMut};

struct Foo;

impl Deref for Foo {
    type Target = FnMut() + 'static;
    fn deref(&self) -> &Self::Target {
        unimplemented!()
    }
} …
Run Code Online (Sandbox Code Playgroud)

rust

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

javafx - 如何在未聚焦的TableView中设置选定的行文本颜色

我的应用程序使用行选择模式(table.setCellSelectionEnabled(false))

在我的CSS中:

.table-row-cell:selected {
   -fx-background-color: steelblue; 
   -fx-text-fill: red !important;
}
Run Code Online (Sandbox Code Playgroud)

财产-fx-background-color工作正常,但-fx-text-fill没有.所选行的文本颜色为黑色(如果TableView未聚焦)或白色(如果TableView聚焦).

css javafx-2

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

如何遍历GDB或LLDB中的Rust枚举字段?

我有以下代码:

struct Foo {
    id: i32,
}
enum Bar {
    Variant(Foo),
}

fn main() {
    let x = Bar::Variant(Foo { id: 100 });
    println!("set breakpoint here");
}
Run Code Online (Sandbox Code Playgroud)

如何打印Foo实例的内存地址?

gdb rust lldb

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

如何从std :: optional <QByteArray>获取QByteArray并离开std :: nullopt而不需要额外的mallocs?

我需要编写一个函数来获取optional<QByteArray>底层数组的所有权.

在Rust中,它是通过这种方式完成的:

fn my_func(f: &mut Option<Box<[u8]>>) -> Box<[u8]> {
     //how to do "take" in c++17?
     f.take().unwrap()
}
Run Code Online (Sandbox Code Playgroud)

我知道std::move,但据我所知,这段代码将为malloc新数组,交换指针和删除不需要的数据QByteArray:

std::optional<QByteArray> optarr = ...
QByteArray optarr = std::move(*myopt);
optarr.reset()
Run Code Online (Sandbox Code Playgroud)

有没有办法和Rust一样(也就是说,没有为临时分配内存QByteArray)?

c++ qt c++17

3
推荐指数
2
解决办法
149
查看次数

如何使 std::variant&lt;..&gt; 可用于 QVariant?

我想std::variant<Foo, Bar>用作QVariant. 这该怎么做?

#include <QCoreApplication>
#include <QMetaType>
#include <variant>

enum Foo{A, B};
enum class Bar{C, D};

Q_DECLARE_METATYPE(std::variant<Foo, Bar>);


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

error: macro "Q_DECLARE_METATYPE" passed 2 arguments, but takes just 1
 Q_DECLARE_METATYPE(std::variant<Foo, Bar>);
                                          ^
Run Code Online (Sandbox Code Playgroud)

c++ qt qt5

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

标签 统计

rust ×6

c++ ×2

qt ×2

c++17 ×1

css ×1

floating-point ×1

formatting ×1

gdb ×1

javafx-2 ×1

lldb ×1

qt5 ×1