我应该在println!宏中使用什么格式字符串才能打印0.0为00000.000?
println!("={:05.3}", 0.0);
Run Code Online (Sandbox Code Playgroud)
输出: =0.000
预期: =00000.000
让我们尝试编译这段代码:
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()失败了?这是编译器错误吗?
为什么这个指针算术(没有读取或写入这些指针后面的数据)是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) 让我们尝试编译这段代码:
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) 我试图可变地借用一个可变变量。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) 我的应用程序使用行选择模式(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聚焦).
我有以下代码:
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实例的内存地址?
我需要编写一个函数来获取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)?
我想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)