我正在尝试创建一个泛型结构,它使用"整数类型"来引用数组.出于性能原因,我希望能够轻松指定是否使用u16
,u32
或u64
.像这样的东西(显然是无效的Rust代码):
struct Foo<T: u16 or u32 or u64> { ... }
Run Code Online (Sandbox Code Playgroud)
有没有办法表达这个?
我编写了以下测试来检查最大可用堆内存:
import java.util.*;
public class Memory {
public static void main(String[] args) {
long maxMB = Runtime.getRuntime().maxMemory() / 1048576L;
System.out.println("Maximum memory is " + maxMB + " MB.");
ArrayList<byte[]> allocated = new ArrayList<>();
try {
while (true)
allocated.add(new byte[1024*1024]);
} catch (OutOfMemoryError e) {
System.out.println("allocated " + allocated.size() + " MB before running out of memory.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我对此进行测试时,似乎实际上只能分配一半的“可用”内存:
$ java -Xmx512m Memory
Maximum memory is 512 MB.
allocated 255 MB before running out of memory.
$ java -Xmx1024m …
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个特征,其中包括在一些内部数据上返回迭代器的函数,然后可以由不同的结构实现。一个最小的例子如下:
trait WrapperTrait {
type WrapperIterator: Iterator<Item=u32>;
fn iter(&self) -> Self::WrapperIterator;
}
struct Wrapper {
data: Vec<u32>
}
impl WrapperTrait for Wrapper {
type WrapperIterator = ...;
fn iter(&self) -> Self::WrapperIterator {
return self.data.iter().map(|&x| x);
}
}
Run Code Online (Sandbox Code Playgroud)
但是, for 的类型WrapperIterator
相当复杂 - 从错误消息(通过类型不匹配创建)我发现它是:
std::iter::Map<std::slice::Iter<'_, u32>, [closure@borrow.rs:45:37: 45:43]>
Run Code Online (Sandbox Code Playgroud)
有没有办法让 Rust 推断出正确的类型WrapperIterator
?
是否有一种简单的方法来注释结构中的字段,以便在导出PartialEq
特征时忽略它们?例如:
#[derive(PartialEq,Eq)]
pub struct UndirectedGraph {
nodes: HashMap<NodeIdx, UndirectedNode>,
// mapping of degree to nodes of that degree
degree_index: Vec<HashSet<NodeIdx>>,
}
Run Code Online (Sandbox Code Playgroud)
我希望两个无向图在它们具有相同的nodes
字段时被认为是相等的,但是degree_index
字段可能不同(向量可能在末尾包含额外的空哈希集).
显然我可以手动实现特性,但自动推导会更简单.
我正在尝试创建宏,以便创建自定义树类型的小实例.为此,我想将子节点指定为节点或整数(没有明确地将它们转换为Node
类型).我的尝试在下面,它与消息的$x
解析类型一样失败.MDTree
expected enum MDTree, found integral variable
pub struct MultiNode {
children: Vec<MDTree>
}
impl MultiNode {
pub fn new(children: Vec<MDTree>) -> Box<MultiNode> {
return Box::new(MultiNode { children: children });
}
}
pub enum MDTree {
Single(u32),
Prime(Box<MultiNode>),
Degenerate(Box<MultiNode>),
}
macro_rules! mod_single {
($x:expr) => { MDTree::Single($x) }
}
macro_rules! mod_multi {
($($x:expr),*) => {{
let mut children: Vec<MDTree> = Vec::new();
$(
match $x {
0...4294967295 => { children.push(mod_single!($x)); }
_ => { …
Run Code Online (Sandbox Code Playgroud) 似乎不可变的scala集合不会缓存它们的hashCode计算(针对immutable.HashSet进行测试),而是每次都重新计算它.是否有任何简单的方法来添加此行为(出于性能原因)?
我已经考虑过创建一个执行缓存的immutable.HashSet的子类,但是没有看到任何方法来实现+ etc.函数来返回一个缓存对象.尽管可能与授权有关,但这看起来非常难看.
在Rust中,向量使用索引usize
,因此在写入时
let my_vec: Vec<String> = vec!["Hello", "world"];
let index: u32 = 0;
println!("{}", my_vec[index]);
Run Code Online (Sandbox Code Playgroud)
你得到一个错误,因为索引应该是类型usize
.我知道这可以通过显式转换索引来修复usize
:
my_vec[index as usize]
Run Code Online (Sandbox Code Playgroud)
但这写起来很乏味.理想情况下,我只是[]
通过实现来重载运算符
impl<T> std::ops::Index<u32> for Vec<T> { ... }
Run Code Online (Sandbox Code Playgroud)
但这是不可能的,因为Rust禁止这样做(因为特征和结构都不是本地的).我能看到的唯一选择是创建一个包装类Vec
,但这意味着必须编写大量的函数包装器.有没有更优雅的方式来解决这个问题?
我正在尝试使用通用数据类型,其中不需要其中一种类型(图中的边权重).我一直在考虑使用never类型,这看起来像这样:
#![feature(never_type)]
struct Foo<T> {
bar: T
}
impl<T> Foo<T> {
fn foo(&mut self, bar: T) {
self.bar = bar;
}
}
fn main() {
let mut foo: Foo<!> = Foo { bar: "nada" };
foo.foo("nada");
}
Run Code Online (Sandbox Code Playgroud)
这显然"nada"
会导致占位符类型不匹配,但只输入任何内容都会导致其他错误.!
这里使用的是正确的类型,如果是,那么正确的语法是什么?
我已经用它()
代替了它!
,但我有点不确定这是否是正确的类型选择.我相信在效率方面它应该没有区别,因为()
没有内存占用?
我正在尝试通过命令行编译以下内容:
import kotlin.test.assertTrue
fun main(args: Array<String>) {
assertTrue(false)
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器失败并显示:
$ kotlinc -d MyCode.jar MyCode.kt
MyCode.kt:1:15: error: unresolved reference: test
import kotlin.test.assertTrue
^
Run Code Online (Sandbox Code Playgroud)
我缺少什么?
当尝试从特征中的默认实现返回特征对象时,例如
trait Foo {
fn foo(self: Box<Self>) -> Box<dyn Foo> {
self
}
}
Run Code Online (Sandbox Code Playgroud)
Rust 编译器抱怨说
error[E0277]: the size for values of type `Self` cannot be known at compilation time
note: required for the cast to the object type `dyn Foo`
Run Code Online (Sandbox Code Playgroud)
类型转换应该是 from Box<Self>
to Box<dyn Foo>
,为什么这需要调整 Self 的大小?
我正在尝试为有向图结构创建一个特征并提供一个非常基本的实现,但是遇到了编译器错误:
pub trait DiGraph<'a> {
type N;
fn nodes<T>(&'a self) -> T where T: Iterator<Item=&'a Self::N>;
fn pre<T>(&'a self, node: Self::N) -> T where T: Iterator<Item=&'a Self::N>;
fn succ<T>(&'a self, node: Self::N) -> T where T: Iterator<Item=&'a Self::N>;
}
struct SimpleNode {
pre: Vec<usize>,
succ: Vec<usize>,
}
pub struct SimpleDiGraph {
pub nodes: Vec<SimpleNode>
}
impl<'a> DiGraph<'a> for SimpleDiGraph {
type N = usize;
fn nodes<T=std::ops::Range<usize>>(&'a self) -> T {
return std::ops::Range { start: 0, end: self.nodes.len() };
}
fn …
Run Code Online (Sandbox Code Playgroud)