是否有可能超载此类呼叫?
f("st");//variant 1
struct Foo { char buf[3]; Foo() { strcpy(buf, "aa"); } };
const Foo foo;
f(foo.buf);//variant 2
Run Code Online (Sandbox Code Playgroud)
在两种情况下typeid给出相同的类型,是否有任何变体来区分f上面的两个调用,并且有两个变体f?我尝试std::is_rvalue_reference但没有成功.
由于输入我有两个Vec Iterator::zip,并xs作为输出我要打印
c1 X,c2 Y.
休息c1 X < - 如果a.len()> b.len()
休息c2 Y < - 如果a.len()<b.len()
一种可能的解决方案是
let xs = vec![1, 2, 3, 4, 5];
let ys = vec![11, 12, 13];
Run Code Online (Sandbox Code Playgroud)
但是对于其他的我需要复杂的代码来检查长度并完成其余部分.
所以我写道:
x=1, y=11
x=2, y=12
x=3, y=13
x=4; no matching Y
x=5; no matching Y
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为ys在这种情况下我有额外的电话:
for (x, y) in xs.iter().zip(ys.iter()) {
println!("x={}, y={}", x, y);
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在迭代器的帮助下解决而不切割其他更大的Vec?
我有这个代码:
pub type f_t =
::std::option::Option<extern "C" fn(a: ::std::os::raw::c_int, ...)>;
#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug)]
pub struct Foo {
pub f: f_t,
}
fn main() {
}
Run Code Online (Sandbox Code Playgroud)
它不能编译,因为Cloneargs末尾的函数没有"..."的默认实现.
Rust如何在行尾处理这个"...",它是否与C中的"..."做同样的事情?
Rust无法编译此代码,但如果我发表评论, ...,它编译得很好.有什么区别,为什么一类函数指针实现Clone而另一类没有呢?
我应该如何实现Clone这些功能?
我知道Rust特有的功能:
Foo {
fieldX: someValue,
..Self::default()
}
Run Code Online (Sandbox Code Playgroud)
我想在一个方法中使用它&mut self,而不是触摸字段f10并将f11其他字符设置为默认值:
#[derive(Default)]
struct Foo {
f1: u32,
//...
f10: Vec<u32>,
f11: Vec<u32>,
}
impl Foo {
fn f1(&mut self) {
let new_me = Foo {
f10: self.f10,
..Self::default()
};
*self = new_me;
}
}
Run Code Online (Sandbox Code Playgroud)
但编译说:
error[E0507]: cannot move out of borrowed content
--> src/main.rs:12:18
|
12 | f10: self.f10,
| ^^^^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
我怎么能够:
f1:&mut selfVec<>,只移动 …我有一个函数f接受两个引用,一个mut和一个不接受mut。我有fa 内的值HashMap:
use std::collections::HashMap;
fn f(a: &i32, b: &mut i32) {}
fn main() {
let mut map = HashMap::new();
map.insert("1", 1);
map.insert("2", 2);
{
let a: &i32 = map.get("1").unwrap();
println!("a: {}", a);
let b: &mut i32 = map.get_mut("2").unwrap();
println!("b: {}", b);
*b = 5;
}
println!("Results: {:?}", map)
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为HashMap::get并HashMap::get_mut尝试同时可变借和不变借:
use std::collections::HashMap;
fn f(a: &i32, b: &mut i32) {}
fn main() {
let mut map = …Run Code Online (Sandbox Code Playgroud) 我的文档中有代码,只有在用户计算机上装有某些软件的情况下,该代码才能运行。为了模拟这一点,我将添加panic!到示例代码中:
//!```rust
//!fn main() {
//! panic!("Not run me");
//!}
//!```
#[cfg(test)]
mod tests {
#[test]
fn it_works() {}
}
Run Code Online (Sandbox Code Playgroud)
我想检查注释中的代码是否可以编译,但是我不想在期间运行它cargo test。现在,我得到:
//!```rust
//!fn main() {
//! panic!("Not run me");
//!}
//!```
#[cfg(test)]
mod tests {
#[test]
fn it_works() {}
}
Run Code Online (Sandbox Code Playgroud)
我读到有关的文章doctest = false,但这不仅使注释中的代码无法运行,而且还禁用了检查注释中的代码的语法。
如何仅在注释中禁用代码运行,而在注释期间仍启用注释中的代码编译cargo test?
我正在使用
$ cargo --version
cargo 0.21.0-beta (7e00b82d9 2017-07-17)
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的项目cargo new --bin test1,然后我添加了一个依赖项:
[dependencies]
lazy_static = "0.2.2"
Run Code Online (Sandbox Code Playgroud)
到Cargo.toml(根据这个版本存在)和
#[macro_use]
extern crate lazy_static;
Run Code Online (Sandbox Code Playgroud)
至 src/main.rs
当我跑cargo build:
$ cargo build
Compiling lazy_static v0.2.8
Compiling test1 v0.1.0 (file:///tmp/test1)
warning: unused `#[macro_use]` import
--> src/main.rs:1:1
|
1 | #[macro_use]
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default
Finished dev [unoptimized + debuginfo] target(s) in 0.49 secs
Run Code Online (Sandbox Code Playgroud)
为什么cargo build编译最后一个版本0.2.8而不是0.2.2我指定的版本?我究竟做错了什么?
代码:
use std::collections::HashSet;
use std::{mem, ptr, fmt};
use std::ops::Deref;
enum Unsafety {
Normal
}
enum ImplPolarity { Positive }
struct TraitRef;
struct Ty;
struct ImplItem;
enum ItemKind {
Impl(Unsafety,
ImplPolarity,
Option<TraitRef>, // (optional) trait this impl implements
Box<Ty>, // self
),
}
struct Item {
node: ItemKind,
}
pub struct P<T: ?Sized> {
ptr: Box<T>
}
impl<T: 'static> P<T> {
pub fn unwrap(self) -> T {
*self.ptr
}
}
impl<T: ?Sized> Deref for P<T> {
type Target = T;
fn …Run Code Online (Sandbox Code Playgroud) 这段代码:
#[allow(dead_code)]
macro_rules! test {
($x:expr) => {{}}
}
fn main() {
println!("Results:")
}
Run Code Online (Sandbox Code Playgroud)
对未使用的宏定义产生以下警告:
warning: unused macro definition
--> /home/xxx/.emacs.d/rust-playground/at-2017-08-02-031315/snippet.rs:10:1
|
10 | / macro_rules! test {
11 | | ($x:expr) => {{}}
12 | | }
| |_^
|
= note: #[warn(unused_macros)] on by default
Run Code Online (Sandbox Code Playgroud)
有可能抑制它吗?正如您所看到的#[allow(dead_code),在宏的情况下无济于事.
这是我模型的简化版本:
class TableModel : public QAbstractTableModel {
public:
TableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {
}
int rowCount(const QModelIndex &parent) const override { return 1; }
int columnCount(const QModelIndex &parent) const override { return 2; }
QVariant data(const QModelIndex &idx, int role) const override { return {}; }
};
Run Code Online (Sandbox Code Playgroud)
如果我以这种方式运行它(使用Qt 模型测试):
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
TableModel tbl_model;
ModelTest mtest{&tbl_model, nullptr};
Run Code Online (Sandbox Code Playgroud)
}
它失败于:
// Common error test #1, make sure that a top level …Run Code Online (Sandbox Code Playgroud)