我正在试图弄清楚如何匹配String
Rust.
我最初尝试过像这样的匹配,但我发现Rust不能暗中强制转换std::string::String
为&str
.
fn main() {
let stringthing = String::from("c");
match stringthing {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
}
}
Run Code Online (Sandbox Code Playgroud)
这有错误:
error[E0308]: mismatched types
--> src/main.rs:4:9
|
4 | "a" => println!("0"),
| ^^^ expected struct `std::string::String`, found reference
|
= note: expected type `std::string::String`
found type `&'static str`
Run Code Online (Sandbox Code Playgroud)
然后我尝试构造新String
对象,因为我找不到将a String
转换为a的函数&str
.
fn main() {
let stringthing = String::from("c");
match stringthing {
String::from("a") => println!("0"),
String::from("b") => …
Run Code Online (Sandbox Code Playgroud) 在Rust中是否可以使用默认参数创建函数?
fn add(a: int = 1, b: int = 2) { a + b }
Run Code Online (Sandbox Code Playgroud) 如何使用C++中的boost创建线程池,如何将任务分配给线程池?
我有一个未知大小的数组,我想获得该数组的一部分并将其转换为静态大小的数组:
fn pop(barry: &[u8]) -> [u8; 3] {
barry[0..3] // mismatched types: expected `[u8, ..3]` but found `&[u8]`
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
在Rust中,main函数定义如下:
fn main() {
}
Run Code Online (Sandbox Code Playgroud)
但是,此函数不允许返回值.为什么一种语言不允许返回值,是否有办法返回一些东西?我是否能够安全地使用C exit(int)
功能,否则会导致泄漏等等?
我想创建一个类似地形的3D噪声发生器,在做了一些研究之后,我得出的结论是,Simplex Noise是迄今为止最好的噪声类型.
我觉得这个名字很容易引起误解,因为我在找到关于这个主题的资源方面遇到了很多麻烦,而且我找到的资源往往写得不好.
我基本上寻找的是一个很好的资源/教程,逐步解释单一噪声的工作原理,并解释如何将其实现到程序中.
我不是在寻找解释如何使用库或其他东西的资源.
有没有办法可以直接从Rust的文件中读取结构?我的代码是:
use std::fs::File;
struct Configuration {
item1: u8,
item2: u16,
item3: i32,
item4: [char; 8],
}
fn main() {
let file = File::open("config_file").unwrap();
let mut config: Configuration;
// How to read struct from file?
}
Run Code Online (Sandbox Code Playgroud)
我如何直接config
从文件中读取配置?这甚至可能吗?
我想尝试D,但我不太确定使用什么编译器.我在这个主题上找到了一些文章和SO问题,但我没有找到任何最新的文章.
每个编译器有什么好处,有什么缺点?现在DMD编译器对我来说似乎是最好的,但我可能会被过时的信息误导.
我想开发一个允许用户通过Tor网络进行通信的应用程序.我做了一些谷歌搜索,但我似乎无法找到API或SDK.如何开发Tor应用程序?那里有图书馆吗?
编辑:我看到没有库或API可用于使用Tor.为了编写这个Tor应用程序,我需要知道什么?
用坐标作为键创建一个std :: map似乎是不可能的.当两个坐标的(x + y + z)相同时,地图只会覆盖前一个坐标.例:
map[Coordinate(1, 0, 0)] = object1;
map[Coordinate(0, 1, 0)] = object2;
map[Coordinate(0, 0, 1)] = object3;
Run Code Online (Sandbox Code Playgroud)
这将导致有一个带有1个元素的std :: map,其中包含object3
value和Coordinate(0, 0, 1)
key.如何防止这种情况,它会包含所有值?
#pragma once
struct Coordinate {
double x, y, z;
Coordinate(double x, double y, double z) : x(x), y(y), z(z) {}
bool operator<(const Coordinate& coord) const {
if(x + y + z < coord.x + coord.y + coord.z)
return true;
return false;
}
bool operator==(const Coordinate& coord) const {
if(x …
Run Code Online (Sandbox Code Playgroud) rust ×5
c++ ×2
api ×1
arguments ×1
arrays ×1
boost ×1
boost-asio ×1
boost-thread ×1
d ×1
dmd ×1
function ×1
gdc ×1
io ×1
match ×1
noise ×1
parameters ×1
perlin-noise ×1
return ×1
sdk ×1
stdmap ×1
string ×1
threadpool ×1
tor ×1
visual-c++ ×1