我需要使用Java获取我的Android设备的MAC地址.我在网上搜索过,但是我找不到任何有用的东西.
我经常Option<String>
从计算中获得一个,我想使用这个值或默认的硬编码值.
这对于一个整数来说是微不足道的:
let opt: Option<i32> = Some(3);
let value = opt.unwrap_or(0); // 0 being the default
Run Code Online (Sandbox Code Playgroud)
但是使用a String
和a &str
,编译器会抱怨类型不匹配:
let opt: Option<String> = Some("some value".to_owned());
let value = opt.unwrap_or("default string");
Run Code Online (Sandbox Code Playgroud)
这里的确切错误是:
error[E0308]: mismatched types
--> src/main.rs:4:31
|
4 | let value = opt.unwrap_or("default string");
| ^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: try using a conversion method: `"default string".to_string()`
|
= note: expected type `std::string::String`
found type `&'static str`
Run Code Online (Sandbox Code Playgroud)
一种选择是将字符串切片转换为拥有的String,如rustc所示:
let value …
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
trait Trait<T> {}
fn foo<'a>(_b: Box<dyn Trait<&'a usize>>) {}
fn bar(_b: Box<dyn for<'a> Trait<&'a usize>>) {}
Run Code Online (Sandbox Code Playgroud)
这两个函数foo
和bar
似乎接受Box<Trait<&'a usize>>
,虽然foo
比更简明做它bar
.他们之间有什么区别?
另外,在什么情况下我需要for<>
像上面那样的语法?我知道Rust标准库在内部使用它(通常与闭包有关),但为什么我的代码需要它呢?
在Rust中,我们可以使用Box<T>
类型在堆上分配内容.此类型用于安全地抽象指向堆内存的指针. Box<T>
由Rust标准库提供.
我很好奇如何Box<T>
实现分配,所以我找到了它的源代码.这是Box<T>::new
(从Rust 1.0开始)的代码:
impl<T> Box<T> {
/// Allocates memory on the heap and then moves `x` into it.
/// [...]
#[stable(feature = "rust1", since = "1.0.0")]
#[inline(always)]
pub fn new(x: T) -> Box<T> {
box x
}
}
Run Code Online (Sandbox Code Playgroud)
实现中唯一的行返回值box x
.box
官方文档中的任何地方都没有解释此关键字; 事实上,它仅在std::boxed
文档页面上简要提及.
我希望管理员为我注册一些DLL,但他可能不想安装整个SDK.
他可以安装gacutil.exe
吗?如果是这样,他在哪里可以得到它?我只是将gacutil.exe
文件通过电子邮件发送给他,他必须把它放在他的机器上才能使用它?
我正在努力学习Qt 5.3,这是我的第一个程序(hello world).当我尝试构建时,它会显示以下错误:
测试表达后的额外字符.
我无法理解为什么会出现这个错误.我刚从互联网上拿了一些简单的代码来检查我是否正确安装了Qt.这是代码:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
错误显示在第3,5,6,8行.我对Qt完全不熟悉,所以请给出一个简单的解释.
我想,这一切都说明了一切.两个命令(pdflatex
和xelatex
)都生成PDF.什么是技术/有效/历史/等.这两个命令之间的区别?我正在使用TeX Live.
为了fun and profit™,我正在用C++编写一个trie类(使用C++ 11标准.)
我trie<T>
有一个迭代器,trie<T>::iterator
.(它们实际上都是函数 const_iterator
s,因为你无法修改trie value_type
.)迭代器的类声明看起来像这样:
template<typename T>
class trie<T>::iterator : public std::iterator<std::bidirectional_iterator_tag, T> {
friend class trie<T>;
struct state {
state(const trie<T>* const node, const typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator& node_map_it ) :
node{node}, node_map_it{node_map_it} {}
// This pointer is to const data:
const trie<T>* node;
typename std::vector<std::pair<typename T::value_type, std::unique_ptr<trie<T>>>>::const_iterator node_map_it;
};
public:
typedef const T value_type;
iterator() =default;
iterator(const trie<T>* node) {
parents.emplace(node, node->children.cbegin());
// ...
}
// ...
private:
std::stack<state> …
Run Code Online (Sandbox Code Playgroud) 在下面的例子将不会编译使用克++ 4.8.2:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v {1, 2, 3};
v.erase(v.cbegin()); // Compiler complains
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器说如下.(它不是很易读,但它抱怨说vector<int>::const_iterator
和之间没有已知的转换vector<int>::iterator
.)
prog.cpp: In function ‘int main()’:
prog.cpp:8:20: error: no matching function for call to ‘std::vector<int>::erase(std::vector<int>::const_iterator)’
v.erase(v.cbegin());
^
prog.cpp:8:20: note: candidates are:
In file included from /usr/include/c++/4.8/vector:69:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/vector.tcc:134:5: note: std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::erase(std::vector<_Tp, _Alloc>::iterator) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, …
Run Code Online (Sandbox Code Playgroud) 在C++中,可以自定义代码std::set
用于对其参数进行排序.默认情况下它使用std::less
,但可以使用Compare
模板参数更改.
Rust BTreeSet
使用Ord
特征对类型进行排序.我不知道如何覆盖这种行为 - 它构建在容器存储的类型的类型约束中.
但是,构建一个按一些本地有用的度量标准排序的项目列表通常是有意义的,但这并不是始终比较项目的最佳方式.或者,假设我想对use
d类型的项目进行排序; 在这种情况下,Ord
即使我愿意,也不可能为类型实现自己.
解决方法当然是建立一个简单的旧Vec
项目,然后是sort
它.但在我看来,这并不像插入时自动排序那样干净.
有没有办法使用Rust的容器类型的替代比较器?