在下面的一段代码中,我试图了解通用生命周期参数'a是如何特化的。
struct Wrapper<'a>(&'a i32);
fn foo() {
let mut r;
{
let x = 0; // the lifetime of x, call it 'x, starts from here |
r = Wrapper(&x); // 'a parameter in Wrapper is specialized to 'x |
drop(r); // |
} // --------------------------------- 'x ends here |
{
let y = 0; // the lifetime of y, call it 'y, starts from here |
// 'y is distinct from 'x |
// neither outlives the …Run Code Online (Sandbox Code Playgroud) 在Rust 1.25.0中获取以下有效代码段:
use std::marker::PhantomData;
trait Foo {
type Eq: Eq;
}
struct Bar<'a>(PhantomData<&'a u8>);
impl<'a> Foo for Bar<'a> {
type Eq = fn(&'a u32) -> u32;
}
Run Code Online (Sandbox Code Playgroud)
正如在操场上看到的那样,这表明Eq已经实现了fn(&'a T) -> T.
如果我们进行非常小的更改,则删除关联类型的生命周期:
use std::marker::PhantomData;
trait Foo {
type Eq: Eq;
}
struct Bar<'a>(PhantomData<&'a u8>);
impl<'a> Foo for Bar<'a> {
type Eq = fn(&u32) -> u32;
}
Run Code Online (Sandbox Code Playgroud)
我们可以看到这Eq是不是为实现fn(&u32) -> u32:
error[E0277]: the trait bound `for<'r> fn(&'r u32) -> u32: …Run Code Online (Sandbox Code Playgroud) 我正在 Windows 上尝试 Rust。我的代码声明并调用外部库中的函数。
声明是这样的:
#[link(name = "Rvea0326nc-64")]
extern "C" {
fn WeibullSpeedProbability(wa: &f32, wk: &f32, qu: &f32, prob: &f32, theerr: &i32) -> ();
}
Run Code Online (Sandbox Code Playgroud)
(都是 ByRef,因为 DLL 是 Fortran。它是用 Intel 编译器构建的。)
请注意,文件名没有扩展名。该 DLL 位于 Rust 项目的 \target\debug\deps 文件夹中。
根据此处的文档 https://doc.rust-lang.org/std/keyword.extern.html,这应该在 Windows 上导入 DLL,但我收到错误,因此:
error: linking with `link.exe` failed: exit code: 1181
<SNIP>
= note: LINK : fatal error LNK1181: cannot open input file 'Rvea0326nc-64.lib'
Run Code Online (Sandbox Code Playgroud)
果然,如果我找到并复制生成 DLL 的 *.lib 文件,一切都会正常。DLL 显然是无关紧要的。
我尝试在链接名称中明确添加“.dll”,但 Rust 只是抱怨它找不到 Rvea0326nc-64.dll.lib。
文档有误吗?我错过了什么吗?有没有办法让 Rust 与 DLL …
此代码段在 Rust 1.26.1 中有效:
use std::ops::AddAssign;
trait Trait
where
for<'a> Self: AddAssign<Self> + AddAssign<&'a Self> + Sized,
{
}
trait Trait2 {
type Associated: Trait;
fn method(u32) -> Self::Associated;
}
fn func<T2: Trait2>() {
let mut t = T2::method(1);
let t2 = T2::method(2);
t += &t2;
}
Run Code Online (Sandbox Code Playgroud)
请注意,它同时Trait实现AddAssign<Self>和AddAssign<&'a Trait>(按此顺序,这在后面很重要)。因此,func我们知道,无论t += t2和t += &t2应该是有效的。正如在操场上看到的,t += &t2是有效的,但使用t += t2不是:
use …Run Code Online (Sandbox Code Playgroud) /**
* Write a description of class TicTacToe here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
public class TicTacToe
{
public static void main(){
Scanner s = new Scanner(System.in);
int state = 0, turn = 1, choice, drawcheck=0;
for(int end=0; end==0; end=end){
System.out.println("\u000c");
for(int i =1; i<10; i++){
if(state%(3^i)==0){
System.out.print(" ");
}
else if(state%(3^i)==1){
System.out.print("X");
}
else{
System.out.print("O");
}
if(i%(3^i)==0){
System.out.println("");
}
else{
System.out.print("|");
}
}
System.out.println("Enter State (max 59048)");
state=s.nextInt(); …Run Code Online (Sandbox Code Playgroud)