在Rust中声明多个"use"语句被认为是不好的风格吗?我是一名C++程序员,最近开始尝试Rust.在我查看Rust代码时,我注意到的一件事是,在许多Rust程序use中,程序顶部会有一堆语句.来自C++,不鼓励using namespace std在制作头文件时特别使用它,但在我见过的大多数Rust程序中似乎并非如此.那么以下哪些琐碎的例子被认为是更好的Rust编程风格?如果您正在制作二进制程序而不是模块,它会改变吗?为什么?
use std::sync::Arc;
use std::sync::Mutex;
use std::thread::Thread;
use std::rand::random;
fn main() {
let mut vec: Vec<char> = (0u8..10).map(|i| i as char).collect();
let mut data = Arc::new(Mutex::new(vec));
for i in 1usize..10 {
let data = data.clone();
let thread = Thread::spawn(move || {
let mut data = match data.lock() {
Ok(guard) => guard,
Err(e) => panic!("{}, was poisoned", e)
};
data.push(random::<char>());
});
}
}
Run Code Online (Sandbox Code Playgroud)
或这个...
fn main() {
let mut vec: Vec<char> = (0u8..10).map(|i| i as …Run Code Online (Sandbox Code Playgroud) 有没有更好的方法为类似联合的类构建移动构造函数?如果我在下面的代码中有类似类的类似类的类,有没有办法构建类或移动构造函数,它不需要像下面代码中的移动构造函数那样的switch语句.
class S {
private:
enum {CHAR, INT, DOUBLE} type; // tag
// anonymous union
union {
char c;
int n;
double d;
};
public:
// constructor if the union were to hold a character
AS(const char c) {
this->tag = AS::CHAR;
this->c = c;
}
// constructor if the union were to hold a int
AS(const int i) {
this->tag = AS::INT;
this->n = i;
}
// constructor if the union were to hold a double
AS(const double d) …Run Code Online (Sandbox Code Playgroud) 当将值存储到指向聚合类型的指针时,getelementptr和store和load和之间有什么区别insertvalue?在某些情况下是首选吗?如果是这样,为什么?还是我完全走错了方向?
; A contrived example
%X = type {i32, i64}
define i32 @main(i32 %argc, i8** %argv) {
entry:
%sX.0 = alloca %X
; change the first value with GEP + store
%val1 = getelementptr %X, %X* %sX.0, i32 0, i32 0
store i32 42, i32* %val1
; change the second value with load + insertvalue
%sX.1 = load %X, %X* %sX.0
%sX.2 = insertvalue %X %sX.1, i64 42, 1
store %X …Run Code Online (Sandbox Code Playgroud) 该link属性使链接到共享库变得多么容易,这给我留下了深刻的印象。但是,我很好奇属性的细节以及它与 C 中的链接相比如何。例如,给定以下 Rust 代码
#[allow(bad_style)]
struct wl_display;
fn main() {
#[link(name="wayland-client", kind="dylib")]
extern {
fn wl_display_connect(name: *const u8) -> *mut wl_display;
}
// do work
}
Run Code Online (Sandbox Code Playgroud)
它会更接近以下 C 代码吗?
#include <stdio.h>
#include <dlfcn.h>
struct wl_display;
int main() {
struct wl_display* (*pwl_display_connect)(const char *name);
char* error;
void* handle = dlopen("/usr/lib/libwayland-client.so", RTLD_LAZY);
if(!handle) {
fprintf(stderr, "Error opening lib: %s\n", dlerror());
exit(1);
}
pwl_display_connect = dlsym(handle, "wl_display_connect");
// do work
if(!pwl_display_connect) {
fprintf(stderr, "Error loading function: %s\n", dlerror());
exit(1);
} …Run Code Online (Sandbox Code Playgroud) 当只有一个可能的前身时,使用phi节点有什么好处?例如,当我运行时opt -loop-<some specific pass> some-cool-file.ll -S,如果我还没有添加一个,那么输出通常会包含一个只有一个可能的前任的phi节点.
例:
endcond.loopexit: ; preds = %loop <- note: one predecessor
%res.lcssa = phi i64 [ %res, %loop ] ; I'm assuming this is from the
br label %endcond ; loop-closed ssa form pass
endcond:
%var = phi i64 [ %res.lcssa, %endcond.loopexit ], <other-pred>
Run Code Online (Sandbox Code Playgroud)
如果只有一个可能的前任不应该与上面完全相同
endcond.loopexit: ; preds = %loop
br label %endcond ; res assigned a value in %loop
endcond:
%var = phi i64 [ %res, %endcond.loopexit ], <other-pred> ; use %res …Run Code Online (Sandbox Code Playgroud) 我正在尝试更多地了解 Rust 中的 FFI 并与 C 库链接,特别是libc. 在我的“任务”中,我遇到了以下问题。
void(* sig_set(int sig, void(*handler)(int))) {
// uninitialized sigaction structs
struct sigaction new_action, old_action;
// assign options to new action
new_action.sa_flags = SA_RESTART;
new_action.sa_handler = handler;
sigemptyset(&new_action.sa_mask);
if(sigaction(sig, &new_action, &old_action) < 0) {
fprintf(stderr, "Error: %s!\n", "signal error");
exit(1);
}
return old_action.sa_handler;
}
Run Code Online (Sandbox Code Playgroud)
use libc; // 0.2.77
fn sig_init(sig: i32, handler: fn(i32) -> ()) -> usize {
unsafe {
let mut new_action: libc::sigaction;
let mut old_action: libc::sigaction;
new_action.sa_flags …Run Code Online (Sandbox Code Playgroud) 我经常使用如下代码来索引数组中的项目,只是为了提醒自己数组值是指向数组第一个元素的指针.
int array[] = {0,1,2,3,4,5};
*(array + 2) = 42;
Run Code Online (Sandbox Code Playgroud)
虽然它看起来有点难看,但实际上我更喜欢传统的[]操作员.
int array[] = {0,1,2,3,4,5};
array[2] = 42;
Run Code Online (Sandbox Code Playgroud)
除了让将来可能会读取我的代码的其他人有点生气之外,使用指针算法在[]运算符上索引数组有什么后果吗?