我试图理解 Rust 中的 Pin https://doc.rust-lang.org/std/pin/。
主要思想很清楚
如果我们有Pin<T>那么类型的指针T就不能移动
Pin我正在编写一个带有和不带有for 的函数Box。我有完全相同的结果。如何修改代码才能看到效果Pin?
use std::fmt::{self, Debug};
use std::pin::Pin;
fn foo_pin<T: Debug>(s: Pin<Box<T>>) {
let copys = s;
println!("Hi from Box: {:?}", copys);
//println!("Hi from Box: {:?}", s); // ERROR borrow of moved value: `s`
}
fn foo<T: Debug>(s: Box<T>) {
let copys = s;
println!("Hi from Box: {:?}", copys);
//println!("Hi from Box: {:?}", s); // ERROR borrow of moved value: `s`
}
fn main() …Run Code Online (Sandbox Code Playgroud) 我正在学习 Rust,想了解函数调用之间的区别
S::foo(&s);
T::foo(&s);
<S as T>::foo(&s);
Run Code Online (Sandbox Code Playgroud)
1 为什么我们需要这些方法?和区别?
2 我们应该在哪里使用它?
struct S;
trait T {
fn foo(&self);
}
impl T for S {
fn foo(&self) {
println!("XXX");
}
}
fn main() {
let s = S;
s.foo(); // (It's clear)
S::foo(&s);
T::foo(&s);
<S as T>::foo(&s);
}
Run Code Online (Sandbox Code Playgroud) 我有一个这样的代码。代码正在工作我知道代码由于去管理而打印“m”(https://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html)
但为什么编译器会打印“m”呢size_t?映射的逻辑是什么 ('i' --> int// 很清楚,但为什么是 'm' --> size_t)
#include <typeinfo>
using namespace std;
int main() {
size_t i = 5;
cout << "Type: " << typeid(i).name() << '\n'; // Type: m
}
Run Code Online (Sandbox Code Playgroud) 我想在 Rust 中迭代两个字符串。我读到使用索引(nth()方法)效率不高。这就是我使用迭代器的原因
fn main() {
let s1 = String::from("abacde");
let s2 = String::from("123456");
let mut iter1 = s1.chars();
let mut iter2 = s2.chars();
while let Some(ch1) = iter1.next() && let Some(ch2) = iter2.next(){
println!("{:?}", ch1);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个错误
错误[E0658]:
let该位置的表达式不稳定
如何修复它?
我有一个代码。代码打印1236(g++ 7.5.0)
输出取决于编译器吗?(例如输出可以是3216)
#include <bits/stdc++.h>
using namespace std;
int foo(int& x) {
std::cout << ++x;
return x;
}
int main() {
int i = 0;
cout << foo(i) + foo(i) + foo(i) << endl; // 1236
}
Run Code Online (Sandbox Code Playgroud)