我写了一些Rust代码&String作为参数:
fn awesome_greeting(name: &String) {
println!("Wow, you are awesome, {}!", name);
}
Run Code Online (Sandbox Code Playgroud)
我还编写了代码来引用a Vec或Box:
fn total_price(prices: &Vec<i32>) -> i32 {
prices.iter().sum()
}
fn is_even(value: &Box<i32>) -> bool {
**value % 2 == 0
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一些反馈意见,这样做并不是一个好主意.为什么不?
是否有相当于alloca在Rust中创建可变长度数组?
我正在寻找相当于以下C99代码:
void go(int n) {
int array[n];
// ...
}
Run Code Online (Sandbox Code Playgroud) 有没有一种很好的方法将Vec<T>大小转换S为类型数组[T; S]?具体来说,我正在使用一个返回128位散列的函数作为a Vec<u8>,它总是长度为16,我想将散列作为a处理[u8, 16].
是否有类似于as_slice方法的东西,它给了我想要的东西,或者我应该编写自己的函数来分配固定大小的数组,迭代复制每个元素的向量,并返回数组?
我正在尝试初始化一些可以为空的非可复制类型的固定大小的数组,就像Option<Box<Thing>>某种类型的那样Thing.我想把它们中的两个打包成一个没有任何额外间接的结构.我想写这样的东西:
let array: [Option<Box<Thing>>; SIZE] = [None; SIZE];
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为[e; n]语法要求e实现Copy.当然,我可以将它扩展为SIZE Nones,但是当SIZE它很大时,这可能是笨重的.我不相信这可以用没有非自然编码的宏来完成SIZE.有没有好办法呢?
是的,这很容易unsafe; 没有办法unsafe吗?
我想创建这样的数组:
let arr = [0; length];
Run Code Online (Sandbox Code Playgroud)
长度是a usize.但是我得到了这个错误
E0307
The length of an array is part of its type. For this reason, this length
must be a compile-time constant.
Run Code Online (Sandbox Code Playgroud)
是否可以创建动态长度的数组?我想要一个数组,而不是一个数组Vec.
我正在从文件中读取原始数据,我想将其转换为整数:
fn main() {
let buf: &[u8] = &[0, 0, 0, 1];
let num = slice_to_i8(buf);
println!("1 == {}", num);
}
pub fn slice_to_i8(buf: &[u8]) -> i32 {
unimplemented!("what should I do here?")
}
Run Code Online (Sandbox Code Playgroud)
我会在C中进行类型转换,但是我在Rust中做什么?
我有一个固定大小的数组结构:
struct PublicHeaderBlock_LAS14 {
file_signature: [u8; 4],
file_source_id: u16,
global_encoding: u16,
project_id_data_1: u32,
project_id_data_2: u16,
project_id_data_3: u16,
project_id_data_4: [u8; 8],
version_major: u8,
version_minor: u8,
systemIdentifier: [u8; 32], // ...
}
Run Code Online (Sandbox Code Playgroud)
我正在从文件中读取字节到固定大小的数组,并将这些字节逐位复制到struct中.
fn create_header_struct_las14(&self, buff: &[u8; 373]) -> PublicHeaderBlock_LAS14 {
PublicHeaderBlock_LAS14 {
file_signature: [buff[0], buff[1], buff[2], buff[3]],
file_source_id: (buff[4] | buff[5] << 7) as u16,
global_encoding: (buff[6] | buff[7] << 7) as u16,
project_id_data_1: (buff[8] | buff[9] << 7 | buff[10] << 7 | buff[11] << 7) as u32,
project_id_data_2: (buff[12] …Run Code Online (Sandbox Code Playgroud) 我有一些Vec不平凡的类型,其大小我确定。我需要将其转换为固定大小的数组。理想情况下我想这样做
Vec问题写成代码:
struct Point {
x: i32,
y: i32,
}
fn main() {
let points = vec![
Point { x: 1, y: 2 },
Point { x: 3, y: 4 },
Point { x: 5, y: 6 },
];
// I would like this to be an array of points
let array: [Point; 3] = ???;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是一个微不足道的问题,但是我无法在Vec文档、Rust Books 的切片部分或通过谷歌搜索中找到令人满意的解决方案。我发现的唯一一件事是首先用零数据初始化数组,然后从 复制所有元素Vec,但这不能满足我的要求。
如果我有一个 type 变量&[u8],有没有办法将其转换为[u8; SIZE]SIZE 是某个固定常量的类型?
我想在a中返回一个向量pub extern "C" fn.由于向量具有任意长度,我想我需要返回一个结构
指向矢量的指针,和
向量中的元素数量
我目前的代码是:
extern crate libc;
use self::libc::{size_t, int32_t, int64_t};
// struct to represent an array and its size
#[repr(C)]
pub struct array_and_size {
values: int64_t, // this is probably not how you denote a pointer, right?
size: int32_t,
}
// The vector I want to return the address of is already in a Boxed struct,
// which I have a pointer to, so I guess the vector is on the heap already.
// …Run Code Online (Sandbox Code Playgroud)