有没有办法尽量减少以下功能检查?
#[cfg(feature = "eugene")]
pub mod eugene_set_steam_id;
#[cfg(feature = "eugene")]
pub mod eugene_balance;
#[cfg(feature = "eugene")]
pub mod eugene_force_map;
#[cfg(feature = "eugene")]
pub mod eugene_rating;
#[cfg(feature = "eugene")]
pub mod eugene_stat;
#[cfg(feature = "eugene")]
pub mod eugene_steam_id;
#[cfg(feature = "eugene")]
pub mod eugene_top;
Run Code Online (Sandbox Code Playgroud)
对于这样的事情:
#[cfg(feature = "eugene")] {
pub mod eugene_set_steam_id;
pub mod eugene_balance;
pub mod eugene_force_map;
pub mod eugene_rating;
pub mod eugene_stat;
pub mod eugene_steam_id;
pub mod eugene_top;
}
Run Code Online (Sandbox Code Playgroud)
这样可以更好地传达意义,更符合人体工程学.
在django 1.7中,collectstatic为我抛出一个例外:
OSError: [Errno 2] No such file or directory: '/static'
Run Code Online (Sandbox Code Playgroud)
我已经执行了很多collectstatic调用,一切正常,但今天有这个问题.
settings.py
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fxblog',
'rest_framework',
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.strip("/"))
STATICFILES_DIRS = (
'/static/',
'/upload/',
)
Run Code Online (Sandbox Code Playgroud)
BASE_DIR是正确的,检查它.目录BASE_DIR/static/exists以及我的所有静态文件.
追溯:
Traceback (most recent call last):
File "../manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File …Run Code Online (Sandbox Code Playgroud) Serde支持应用用于以下内容的自定义属性#[derive(Serialize)]:
#[derive(Serialize)]
struct Resource {
// Always serialized.
name: String,
// Never serialized.
#[serde(skip_serializing)]
hash: String,
// Use a method to decide whether the field should be skipped.
#[serde(skip_serializing_if = "Map::is_empty")]
metadata: Map<String, String>,
}
Run Code Online (Sandbox Code Playgroud)
我理解如何实现一个过程宏(Serialize在这个例子中),但我该怎么做才能实现#[serde(skip_serializing)]?我无法在任何地方找到这些信息.该文档甚至没有提到这一点.我试着看一下serde-derive源代码,但对我来说这很复杂.
是否在Rust中定义了函数参数评估的顺序?
fn f(a: u64, b: u64, c: u64) {}
fn g() -> u64 { 0 }
fn h() -> u64 { 1 }
fn i() -> u64 { 2 }
fn main() {
f(g(), h(), i());
}
Run Code Online (Sandbox Code Playgroud)
另外,我担心结构的初始化顺序:
fn f() {}
fn g() {}
A {
a: f(),
b: g(),
}
Run Code Online (Sandbox Code Playgroud)
是保证顺序a,然后b?
对于我的特定用例,我将diesel像这样在事务中初始化一个结构:
db_connection.transaction(||
Ok(CompanyAndUser {
company: companies::register_company(...)?, // performs diesel insert
user: users::register_user(...)?, // performs diesel insert
})
);
Run Code Online (Sandbox Code Playgroud)
显然,我希望对事务中的这两个柴油调用进行排序。不幸的是,我没有找到任何有关此的信息。另外,我发现了一些或多或少的相关信息,但是它已经很老了。
我在git存储库中托管了一个Rust项目,我想让它在某个命令上打印出版本.如何将该版本包含在程序中?我认为构建脚本可以设置环境变量,可以在编译项目本身时使用,但它不起作用:
build.rs:
use std::env;
fn get_git_hash() -> Option<String> {
use std::process::Command;
let branch = Command::new("git")
.arg("rev-parse")
.arg("--abbrev-ref")
.arg("HEAD")
.output();
if let Ok(branch_output) = branch {
let branch_string = String::from_utf8_lossy(&branch_output.stdout);
let commit = Command::new("git")
.arg("rev-parse")
.arg("--verify")
.arg("HEAD")
.output();
if let Ok(commit_output) = commit {
let commit_string = String::from_utf8_lossy(&commit_output.stdout);
return Some(format!("{}, {}",
branch_string.lines().next().unwrap_or(""),
commit_string.lines().next().unwrap_or("")))
} else {
panic!("Can not get git commit: {}", commit_output.unwrap_err());
}
} else {
panic!("Can not get git branch: {}", branch.unwrap_err());
}
None
}
fn main() { …Run Code Online (Sandbox Code Playgroud) 我试图用我的二进制文件(用Rust编写)创建一个图像,但我得到了不同的错误.这是我的Dockerfile:
FROM scratch
COPY binary /
COPY .env /
COPY cert.pem /etc/ssl/
ENV RUST_BACKTRACE 1
CMD /binary
Run Code Online (Sandbox Code Playgroud)
建筑完成很好但是当我尝试运行它时我得到了这个:
$ docker run binary
docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"/bin/sh\": stat /bin/sh: no such file or directory": unknown.
ERRO[0000] error waiting for container: context canceled
Run Code Online (Sandbox Code Playgroud)
还有这个:
$ docker run binary /binary
standard_init_linux.go:195: exec user process caused "no such file or directory"
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做.错误消息对我来说很奇怪.根据官方Docker文档,它必须工作.
系统信息:最新的Arch Linux和Docker:
Docker version 18.02.0-ce, build …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
struct A {
names: Vec<String>,
}
impl ToString for A {
fn to_string(&self) -> String {
// code here
}
}
fn main() {
let a = A {
names: vec!["Victor".to_string(), "Paul".to_string()],
};
println!("A struct contains: [{}].", a.to_string());
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
结构包含:[Victor,Paul].
实现这一特性以实现目标的最佳方法是什么?我尝试了一些奇怪的'每个'和其他变种,但每个变量都给我一个这样的尾随逗号:
维克多,保罗,
当然我可以稍后把它弹出来,但我对这种语言感兴趣所以我想知道这个的最佳实践.这只是我尝试过的一个例子,但没关系,我问的是如何最有效地做到这一点.
我有以下代码(playground):
struct A {
pub vec: Vec<u64>,
}
impl A {
fn perform_for_all<F: Fn(&mut u64)>(&mut self, f: F) {
for mut i in &mut self.vec {
f(i);
}
}
}
fn main() {
let mut a = A {
vec: vec![1, 3, 44, 2, 4, 5, 6],
};
let mut done = false;
a.perform_for_all(|v| {
println!("value: {:?}", v);
done = true;
});
if !done {
a.perform_for_all(|v| {
println!("value {:?}", v);
});
}
}
Run Code Online (Sandbox Code Playgroud)
发生以下错误:
error[E0594]: cannot assign to …Run Code Online (Sandbox Code Playgroud) 我有这个代码(游乐场):
use std::sync::Arc;
pub trait Messenger : Sync + Send {
fn send_embed<F: FnOnce(String) -> String>(&self, u64, &str, f: F)
-> Option<u64> where Self: Sync + Send;
}
struct MyMessenger {
prefix: String,
}
impl MyMessenger {
fn new(s: &str) -> MyMessenger {
MyMessenger { prefix: s.to_owned(), }
}
}
impl Messenger for MyMessenger {
fn send_embed<F: FnOnce(String) -> String>(&self, channel_id: u64, text: &str, f: F) -> Option<u64> {
println!("Trying to send embed: chid={}, text=\"{}\"", channel_id, text);
None …Run Code Online (Sandbox Code Playgroud) 是否可以设置一个值的最小和最大限制(假设它是无符号短,我需要一个0到10之间的值)因为我可以设置默认值
opt::value<unsigned short>()->default_value(5)
Run Code Online (Sandbox Code Playgroud)
我想立即使用从程序选项的变量映射给出的参数而不检查它们中的每一个.