简体   繁体   English

在Rust中解释C声明

[英]Explaining C declarations in Rust

I need to rewrite these C declarations in Go and Rust for a set of practice problems I am working on. 对于我正在处理的一系列实践问题,我需要在Go和Rust中重写这些C声明。 I figured out the Go part, but I am having trouble with the Rust part. 我想通了Go部分,但是在Rust部分遇到了麻烦。 Any ideas or help to write these in Rust? 有任何想法或帮助在Rust中编写这些想法吗?

  1. double *a[n]; 双* a [n];
  2. double (*b)[n]; 双(* b)[n];
  3. double (*c[n])(); 双(* c [n])();
  4. double (*d())[n]; 双(* d())[n];

The answer depends on what, exactly, the * is for. 答案取决于*确切含义。 For example, is the first one being used as an array of pointers to double s, or is it an array of arrays of double s? 例如,是被用来作为一个指针数组与第一个double S,或者是它的数组的数组double S' Are the pointers nullable or not? 指针是否可以为空?

Also, is n a constant or not? 另外, n为常数? If it is, then you want an array; 如果是,则需要一个数组。 if it's not, you want a Vec . 如果不是,则需要Vec

Also also, are these global or local declarations? 另外,这些是全局还是局部声明? Are they function arguments? 它们是函数参数吗? There's different syntax involved for each. 每种语法都有不同的语法。

Frankly, without more context, it's impossible to answer this question with any accuracy. 坦白地说,没有更多的背景信息,就不可能准确地回答这个问题。 Instead, I will give you the following: 相反,我将为您提供以下内容:

  • The Rust documentation contains all the information you'll need, although it's spread out a bit. Rust文档包含了您所需要的所有信息,尽管它散布了一些。 Check the reference and any appropriate-looking guides. 检查参考资料和任何外观合适的指南。 The FFI Guide is probably worth looking at. FFI指南》可能值得一看。

  • cdecl is a website that will unpick C declarations if that's the part you're having difficulty with. cdecl是一个网站,如果您遇到困难,它将取消选择C声明。 Just note that you'll have to remove the semicolon and the n or it won't parse. 请注意,您必须删除分号和n ,否则它将无法解析。

  • The floating point types in Rust are f32 and f64 , depending on whether you're using float or double . Rust中的浮点类型为f32f64 ,具体取决于您使用的是float还是double Also, don't get caught: int in Rust is not equivalent to int in C. Prefer explicitly-sized types like i32 or u64 , or types from libc like c_int . 另外,请不要陷入困境:Rust中的int 等同于C中的int 。建议使用显式大小的类型(例如i32u64 )或libc类型(例如c_int int and uint should only be used with explicitly pointer-sized values. intuint应该与显式指针大小的值一起使用。

  • Normally, you'd write a reference to a T as &T or &mut T , depending on desired mutability (default in C is mutable, default in Rust is immutable ). 通常,您会根据所需的可变性将对T的引用编写为&T&mut T (C中的默认值是可变的,Rust中的默认值是不可变的 )。

  • If you want a nullable reference, use Option<&T> . 如果要使用可为空的引用,请使用Option<&T>

  • If you are trying to use these in a context where you start getting complaints about needing "lifetimes"... well, you're just going to have to learn the language. 如果您试图在开始抱怨需要“一生”的环境中使用这些语言……那么,您将只需要学习该语言。 At that point, simple translation isn't going to work very well. 那时,简单的翻译将无法很好地工作。

  • In Rust, array types are written as brackets around the element type. 拉斯特,数组类型被写入为围绕元件型支架。 So an "array of double s" would be [f64] , an array of size n would be [f64, ..n] . 因此,“ double s数组”为[f64] ,大小为n的数组为[f64, ..n] n [f64, ..n] Typically, however, the actual equivalent to, say, double[] in C would be &[f64] ; 但是,通常, 实际上,等效于C中的double[]&[f64] that is, a reference to an array, rather then the actual contents of the array. 即是对数组的引用,而不是对数组的实际内容的引用。

  • Use of "raw pointers" is heavily discouraged in Rust, and you cannot use them meaningfully outside of unsafe code. 在Rust中, 强烈建议不要使用“原始指针”,并且您不能在不安全的代码之外有意义地使用它们。 In terms of syntax, a pointer to T is *const T or *mut T , depending on whether it's a pointer to constant or mutable data. 在语法上,指向T的指针是*const T*mut T ,具体取决于它是指向常量还是可变数据的指针。

  • Function pointers are just written as fn (Args...) -> Result . 函数指针只是写为fn (Args...) -> Result So a function that takes nothing and returns a double would be fn () -> f64 . 因此,什么都不做并返回双fn () -> f64的函数将是fn () -> f64

Assuming n is a constant: 假设n是一个常数:

let a: [*mut f64, ..n];           // double *a[n];
let b: *mut [f64, ..n];           // double (*b)[n];
let c: [fn() -> f64, ..n];        // double (*c[n])();
fn d() -> *mut [f64, ..n];        // double (*d())[n];

These are rather awkward and unusual types in any language. 在任何语言中,这些都是相当尴尬且不寻常的类型。 Rust's syntax, however, makes these declarations a lot easier to read than C's syntax does. 然而,Rust的语法使这些声明比C的语法更容易阅读。

Note that d in C is a function declaration. 请注意,C中的d是函数声明。 In Rust, external function declarations are only allowed in extern blocks (see the FFI guide ). 在Rust中,仅在extern块中允许使用外部函数声明(请参见FFI指南 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM