我有这样的代码:
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("mode1", "")
("mode2", "");
po::variables_map var_map;
po::store(po::parse_command_line(argc, argv, desc), var_map);
po::notify(var_map);
Run Code Online (Sandbox Code Playgroud)
并且我的程序只能在mode1或mode2中工作.而且我不想要这样的语法--mode 0/1,因为在语义上这是完全不同的东西.
因此,可以告诉boost program_options模块实现这样的语义
没有选项 - >错误
mode1 - >好的
mode2 - >好的
mode1 mode2 - >错误
?
注意:是的,我之后可以使用var_map.count并检查这个po::notify,但我
在文档中看到了类似http://www.boost.org/doc/libs/1_59_0/doc/html/boost/program_options/value_semantic.html的类,我想我可以使用program_options中的东西来检查这种语义.
我使用标准方式(根据Rust书)编写单元测试:
fn func() -> i32 {
0
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn func_test() {
let res = func();
}
}
Run Code Online (Sandbox Code Playgroud)
为了使它编译,我必须func通过pub关键字公开.
是否可以func私有,但在内部测试模块中使用它?
我有以下代码:
#[derive(Clone, Copy)]
struct Point {
x: f64,
y: f64,
z: f64
}
impl Point {
fn set_z(&self, val: f64) -> Point {
Point{x: self.x, y: self.y, z: val}
}
}
struct Boo {
point: Point
}
impl Boo {
fn point(&self) -> &Point { &self.point }
fn set_point(&mut self, val: &Point) { self.point = *val; }
}
fn main() {
let mut boo = Boo{point:Point{x: 0., y: 0., z: 0.}};
let new_p = boo.point().set_z(17.);
boo.set_point(&new_p);
}
Run Code Online (Sandbox Code Playgroud)
事实上,我有一堆像字段这样的结构Boo, …
我有a的形式的纬度,f64我需要将它转换成一个String.起初我想过实现Display,像这样:
struct Latitude(f64);
impl fmt::Display for Latitude {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", if self.0 > 0. { "N" } else { "S" }, self.0)
}
}
fn main() {
let lat: f64 = 45.;
println!("{}", Latitude(lat));
}
Run Code Online (Sandbox Code Playgroud)
在那之后,我有额外的要求.我需要转换为两种表示形式之一:
N 70.152351N 70° 09' 08"还有一个额外的旗帜; 什么时候false,我需要这样的东西:
- --.------ - --° -' -"实现这一目标的最简单方法是:
fn format_lat(degree: f64, use_min_sec_variant: bool, is_valid: bool) -> String;
Run Code Online (Sandbox Code Playgroud)
但是,我在Rust标准库中看不到任何自由函数.
也许我应该使用struct …
我在单独的线程中运行一些代码:
fn f1() -> Result<(), String> { Err("err1".to_string()) }
fn f2() -> Result<(), String> { Err("err2".to_string()) }
::std::thread::spawn(move || {
f1().expect("f1 failed");
f2().expect("f2 failed");
});
Run Code Online (Sandbox Code Playgroud)
我也用日志箱进行日志记录。
结果我想要panic!打电话error!而不是print!。
我找到了恐慌钩子,但我不喜欢unwrap这样的代码:
panic::set_hook(Box::new(|panic_info| {
error!("panic occured: {:?}", panic_info.payload().downcast_ref::<&str>().unwrap());
}));
Run Code Online (Sandbox Code Playgroud)
我担心:
这通常(但并非总是)是
&'static str或String
但不会downcast总是成功吗?
我现在没有Windows机器,但我想让我的代码跨平台.我有工作代码build.rs在Linux上工作:
Path::new("dir1/dir2/dir3")
Run Code Online (Sandbox Code Playgroud)
这对Windows是正确的还是我应该使用类似的东西:
Path::new("dir1").join("dir2").join("dir3")
Run Code Online (Sandbox Code Playgroud) 假设我有Foo课:
struct Resource {
void block();
void unblock();
};
struct Foo {
static Foo create() {
Resource resource;
resource.block();
return Foo{resource};
}
~Foo() { resource.unblock(); }
void f() {}
private:
Resource resource;
Foo(Resource resource): resource(resource) {}
};
Run Code Online (Sandbox Code Playgroud)
我是对的并且不能保证~Foo在这样的块中只会被调用一次吗?
{
Foo foo = Foo::create();
foo.f();
}
Run Code Online (Sandbox Code Playgroud)
如果没有保证,如果使用 c++11 和移动语义是否有可能以某种方式修复?例如,不调用unblock_resource移动的 foo,但我不确定是否保证使用移动构造函数/运算符 = 来返回Foo::create?
如果文件不是带有 和 的 utf-8 编码,是否可以逐行读取std::io::File文件std::io::BufReader?
我查看std::io::Lines并返回Result<String>,所以我担心,我是否实现了自己的功能,BufReader执行相同的操作,但返回,或者我可以以某种方式Vec<u8>重用?std::io::BufReader
我在64位Linux上试过这个,它给出了16:
println!("Results: {}", mem::size_of::<Option<f64>>())
Run Code Online (Sandbox Code Playgroud)
据我了解,这是因为:
pub struct Discriminant<T>(u64, PhantomData<fn() -> T>);
Run Code Online (Sandbox Code Playgroud)
64位判别式的含义是什么?对于手写的代码,256就足够了,对于生成的代码,2 ^ 16将是一个巨大的数字,我甚至无法想象为什么需要2 ^ 32.为什么它会使用64位呢?
为什么编译器没有根据Option情况对其进行优化?结构末端的8位应该足够了.
这是一个玩具示例:
#[derive(Debug)]
struct Int {
v: i32,
}
#[derive(Debug)]
struct Double {
v: f64,
}
impl Into<Double> for Int {
fn into(self) -> Double {
Double {
v: f64::from(self.v),
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个工作,但我真的要实现Into<Double>了&Int和&mut Int.这不起作用:
impl<T> Into<Double> for T
where
T: AsRef<Int>,
{
fn into(self) -> Double {
Double {
v: f64::from(self.as_ref().v),
}
}
}
Run Code Online (Sandbox Code Playgroud)
因为trait Into我的箱子里没有定义:
error[E0119]: conflicting implementations of trait `std::convert::Into<Double>`:
--> src/main.rs:19:1
|
19 | / impl<T> Into<Double> for T …Run Code Online (Sandbox Code Playgroud)