我正在使用一个复杂的密钥,HashMap使得密钥包含两个部分,一个部分是a String,我无法弄清楚如何通过该HashMap::get方法进行查找而不String为每个查找分配新的.
这是一些代码:
#[derive(Debug, Eq, Hash, PartialEq)]
struct Complex {
n: i32,
s: String,
}
impl Complex {
fn new<S: Into<String>>(n: i32, s: S) -> Self {
Complex { n: n, s: s.into() }
}
}
fn main() {
let mut m = std::collections::HashMap::<Complex, i32>::new();
m.insert(Complex::new(42, "foo"), 123);
// OK, but allocates temporary String
assert_eq!(123, *m.get(&Complex::new(42, "foo")).unwrap());
}
Run Code Online (Sandbox Code Playgroud)
问题出在最后的断言上.它通过,但它需要临时堆分配,因为我不能构造一个Complex没有构造一个String.
为了消除这样的临时分配,Rust提供了Borrow该HashMap::get方法使用的特征.我理解如何Borrow为简单的键工作.例如,Rust标准库的PathBuf实现Borrow<Path> …
我有一个关于Rust中的泛型的新手问题(版本1.0).
假设我写了一个通用函数来进行除法.别担心这种功能的用处; 这个问题很简单,这是一个简单的功能.
fn divide<T: std::ops::Div>(a: T, b: T) -> T {
a / b
}
fn main() {
println!("{}", divide(42, 18))
}
Run Code Online (Sandbox Code Playgroud)
该程序无法编译.
src/main.rs:2:5: 2:10 error: mismatched types:
expected `T`,
found `<T as core::ops::Div>::Output`
(expected type parameter,
found associated type) [E0308]
src/main.rs:2 a / b
^~~~~
Run Code Online (Sandbox Code Playgroud)
我知道编译器错误告诉我除法运算的结果是类型Output,而不是T,我Output在标准库文档中看到了类型.
我如何转换Output为T?我试着as用来演员.
fn divide<T: std::ops::Div>(a: T, b: T) -> T {
(a / b) as T
}
fn main() …Run Code Online (Sandbox Code Playgroud) 在Visual Studio中指定/mdd(多线程调试dll)和/mtd(多线程调试)之间有什么区别?
请考虑以下代码:
ifstream in;
try {
in.exceptions ( ifstream::failbit | ifstream::badbit );
in.open(pConfLocation);
} catch ( ifstream::failure e ) {
throw std::runtime_error("Can't open configuration file\n");
}
vector<string> lns;
string s;
in.clear();
while ( !in.eof() ){
getline( in, s );
boost::algorithm::trim(s);
lns.push_back( s+='\n');
}
Run Code Online (Sandbox Code Playgroud)
所以:
异常掩码是所有流对象的内部值,指定哪些状态标志在设置时必须抛出异常.
我没有设置ifstream :: eofbit,但无论如何在运行时出现以下错误:
terminate called after throwing an instance of 'std::ios_base::failure'
what(): basic_ios::clear
The program has unexpectedly finished.
Run Code Online (Sandbox Code Playgroud)
我无法理解这种行为.我试图在{}之前使用in.clear() …
由于使用了相关类型,我在Rust 1.14中遇到了一生的错误,由以下两个类似的程序演示,第一个编译没有错误,第二个编译有生命周期错误.
程序#1 - 编译没有错误
trait Trait<'a> {
type T;
}
struct Impl;
impl<'a> Trait<'a> for Impl {
type T = std::marker::PhantomData<&'a ()>;
}
struct Alpha<'a, T: Trait<'a>> {
_dummy: std::marker::PhantomData<(&'a (), T)>,
}
fn use_alpha<'a>(_: &'a Alpha<'a, Impl>) {}
fn main() {
for x in Vec::<Alpha<Impl>>::new().into_iter() {
use_alpha(&x); // <-- ok
}
}
Run Code Online (Sandbox Code Playgroud)
程序#2 - 有生命周期错误
trait Trait<'a> {
type T;
}
struct Impl;
impl<'a> Trait<'a> for Impl {
type T = std::marker::PhantomData<&'a ()>;
}
struct Alpha<'a, T: Trait<'a>> …Run Code Online (Sandbox Code Playgroud) 我在链接应用程序时在MFC VS6项目中遇到这些类型的错误:
msvcrt.lib(MSVCRT.dll) : error LNK2005: _atoi already defined in LIBC.lib(atox.obj)
Run Code Online (Sandbox Code Playgroud)
我知道它意味着什么(一个函数存在于两个不同的库中); 解决它我应该排除2个库(msvcrt.lib或libc.lib)中的一个.
但是,如果我这样做,会有各种未解决的外部错误.所以我想继续使用这两个库.
有没有办法告诉链接器我想要使用该_atoi函数libc.lib而不是msvcrt.lib(或其他方式)?
任何帮助或方向都会很棒.
是否可以使用该cargo命令来运行库测试(即cargo test --lib)和文档测试,而无需运行任何集成测试(即板条箱顶级tests目录中的测试)?在不运行积分的情况下编译积分测试会获得积分。
这是更大的图景。我的板条箱是Web服务的客户端库,而HTTP服务器不属于板条箱。我已将板条箱整理成:
因此,有时在构建箱子的计算机上运行HTTP服务器是不可行的,例如Travis CI构建代理。在这些情况下,我希望构建所有测试,但要排除所有集成测试的运行,因为每个集成测试都会失败。
我在 Boost ASIO 文档和 StackOverflow 上读到的所有内容都表明我可以async_accept通过调用close接受器套接字来停止操作。但是,当我尝试执行此操作时,处理程序not_socket中出现间歇性错误。async_accept我做错了什么或者 Boost ASIO 不支持这个吗?
(注意:我在 Windows 7 上运行并使用 Visual Studio 2015 编译器。)
\n\n我面临的核心问题是async_accept接受传入连接的操作与我对close. 即使使用显式或隐式的链时也会发生这种情况。
请注意,我对的调用async_accept严格发生在对 的调用之前close。我得出的结论是,竞争条件存在于我的调用close与 Boost ASIO 中接受传入连接的底层代码之间。
我已经包含了演示该问题的代码。该程序重复创建一个接受器,连接到它,然后立即关闭该接受器。它期望async_accept操作成功完成或被取消。任何其他错误都会导致程序中止,这就是我间歇性看到的情况。
为了同步,程序使用显式链。然而,对的调用与操作的效果close不同步,因此有时接受器在接受传入连接之前关闭,有时在之后关闭,有时两者都不是\xe2\x80\x94,因此出现问题。async_accept
这是代码:
\n\n#include <algorithm>\n#include <boost/asio.hpp>\n#include <cstdlib>\n#include <future>\n#include <iostream>\n#include <memory>\n#include <thread>\n\nint main()\n{\n boost::asio::io_service ios;\n auto work …Run Code Online (Sandbox Code Playgroud) 据我了解,
std::bind 完美地转发它包装的可调用对象和该可调用对象的参数;std::bind返回对象本身是可移动的和/或能够复制,这取决于可调用对象和其参数是否是可移动的和/或能够复制;std::bind返回对象可以是嵌套的,在这种情况下,外部std::bind返回对象是可移动的和/或能够复制,正如结合其他可调用的对象时.因此,我希望以下代码片段可以编译好.相反,代码会在最后两个语句中生成一堆编译器错误main().
#include <functional>
template<typename HandlerType>
void call_handler(HandlerType&& handler)
{
handler();
}
template<typename HandlerType>
void do_something(HandlerType&& handler)
{
auto f = std::bind(
&call_handler<HandlerType&>,
std::forward<HandlerType>(handler));
f();
}
int main()
{
auto a = [&]() {};
do_something(a);
do_something(std::move(a));
auto b = std::bind([&]() {});
do_something(b); // <- compiler error!
do_something(std::move(b)); // <- compiler error!
}
Run Code Online (Sandbox Code Playgroud)
两个问题行中的每一个都没有出现另一个错误.为了消除所有错误,我必须注释掉这两行.
这是一个示例错误,来自Cygwin中的g ++ 4.9.2,在调用f()in do_something():
(4 of 103): error: no match for call …Run Code Online (Sandbox Code Playgroud) 我想专门&'static str从&'a str.像这样的东西:
use std::borrow::Cow;
struct MyString {
inner: Cow<'static, str>,
}
impl From<&'static str> for MyString {
fn from(x: &'static str) -> Self {
MyString {
inner: Cow::Borrowed(x),
}
}
}
impl<T: Into<String>> From<T> for MyString {
fn from(x: T) -> Self {
MyString {
inner: Cow::Owned(x.into()),
}
}
}
fn main() {
match MyString::from("foo").inner {
Cow::Borrowed(..) => (),
_ => {
panic!();
}
}
let s = String::from("bar");
match MyString::from(s.as_ref()).inner {
Cow::Owned(..) => (), …Run Code Online (Sandbox Code Playgroud) 我在编译Rust代码时遇到了问题,我设法将问题归结为这个代码片段:
use std::slice::Iter;
pub trait Foo<'a> {
type Bar: Iterator<Item=&'a usize>;
fn make(&self) -> usize;
}
pub struct Juice;
impl <'a> Foo<'a> for Juice {
type Bar = Iter<'a, usize>;
fn make(&self) -> usize { 0us }
}
// Uncomment this line to break things
// fn get_int<'a, T: Foo<'a>>(t: T) -> usize {
// t.make()
// }
fn main() {
println!("Hello, {:?} world!" , Juice.make());
}
Run Code Online (Sandbox Code Playgroud)
我很确定我只是遗漏了一些东西,我需要做些什么来使这个特性发挥作用?我正在使用最新的每晚alpha版本(在撰写本文时):
rustc 1.0.0-nightly (458a6a2f6 2015-01-25 21:20:37 +0000)
Run Code Online (Sandbox Code Playgroud) 我尝试在Visual Studio 2013中编译以下程序并得到C2686: cannot overload static and non-static member functions错误.
#include <iostream>
#include <type_traits>
struct foo
{
template<bool P>
static std::enable_if_t<P> bar()
{
std::cerr << "P is true\n";
}
template<bool P>
std::enable_if_t<!P> bar()
{
std::cerr << "P is false\n";
}
};
int main()
{
foo f;
f.bar<true>();
}
Run Code Online (Sandbox Code Playgroud)
我熟悉这个编译器错误 - 请参阅此StackOverflow答案,但是很惊讶地发现错误与SFINAE一起,编译器将始终丢弃过载集中的两个重载中的一个.
Visual Studio 2013是否正确遵循此处的标准,或者是否可以与SFINAE一起使静态过载?
编辑:上面的对比示例与返回类型重载
如果没有SFINAE,则无法重载static,也无法在返回类型上重载.但是,Visual Studio 2013支持与SFINAE一起在返回类型上重载.
以下程序与上面的程序相同但删除static并更改了第二个foo::bar声明的返回类型,正确编译.
#include <iostream>
#include <type_traits>
struct foo
{
template<bool P>
std::enable_if_t<P> bar() …Run Code Online (Sandbox Code Playgroud) 我在 openSUSE 上使用 Git,并在尝试通过 HTTPS 克隆 Git 存储库时收到以下错误。
$ git clone https://example.com/foo.git
Cloning into 'foo'...
fatal: unable to access 'https://example.com/foo.git/': SSL certificate problem: unable to get local issuer certificate
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我是一名 openSUSE 新手,但了解其他 Linux 发行版。