Some data: improved loaded gRPC server throughput by 15% & reduced p99 latency by 36%; reduced worst-case tail latency by up to 2,000x (429 ms ⇒ 192 us) in application benchmx; reduced latency 25% & improved throughput 20% in application benchmx; improved microbenchmx up to 15%
这里 c 转化成 iterator::IntoIterator 并调用 into_iter() 方法,由于 Counter 没有实现 Copy trait,c 也会移动到 iter 可变变量上,所以这里就可以修改内部变量。
内部会不断循环调用,知道 next 方法返回 None。
所以 for...in 在类型没有实现 Copy 的时候会移动变量,后续也不能再使用这个变量。
1 2 3 4 5 6 7 8 9 10 11 12
fnmain() { letc = Counter { count: 0 }; foriin c { println!("{}", i); } println!("{}", c.count); // Error // borrow of moved value: `c` // value borrowed here after moverustc(E0382) // main.rs(19, 14): value moved here // main.rs(18, 9): move occurs because `c` has type `Counter`, which does not implement the `Copy` trait // main.rs(19, 14): consider borrowing to avoid moving into the for loop }
如果我们为 Counter 加上 Copy 和 Clone trait,那么这里就不会报错了,但是由于 for 循环仅仅复制了 c 数据,所以最后还是打印值为 0
/// An ed25519 keypair. #[derive(Debug, Default)]// we derive Default in order to use the clear() method in Drop pubstructKeypair { /// The secret half of this keypair. pub secret: SecretKey, /// The public half of this keypair. pub public: PublicKey, } implKeyPair { pubfngenerate<R>(csprng: &mut R) -> Keypair where R: CryptoRng + RngCore, { letsk: SecretKey = SecretKey::generate(csprng); letpk: PublicKey = (&sk).into();
Keypair{ public: pk, secret: sk } } }
/// An EdDSA secret key. #[derive(Default)]// we derive Default in order to use the clear() method in Drop pubstructSecretKey(pub(crate) [u8; SECRET_KEY_LENGTH]);
function deposit() public payable { wallet.deposit{value: msg.value}(); } function withdraw() public payable { require(msg.sender == owner); wallet.withdraw(wallet.balanceOf(address(this))); }
fallback() external payable { if (msg.sender == address(wallet)){ uint balance = wallet.balanceOf(address(this)); if (msg.sender.balance >= balance){ wallet.withdraw(balance); } } }
function flush() public { require(msg.sender == owner); selfdestruct(owner); } }
funcmain() { var x interface{} = (*int)(nil) var y interface{} = (*float64)(nil)
fmt.Println(x == y) // false var i interface{} = 1 var j interface{} = 1 fmt.Println(i == j) // true var a interface{} var b interface{} fmt.Println(a == b) // true }
contract FeedConsumer { DataFeed feed; uint errorCount; function rate(address token) public returns (uint value, bool success) { // Permanently disable the mechanism if there are // more than 10 errors. require(errorCount < 10); try feed.getData(token) returns (uint v) { return (v, true); } catch Error(string memory /*reason*/) { // This is executed in case // revert was called inside getData // and a reason string was provided. errorCount++; return (0, false); } catch (bytes memory /*lowLevelData*/) { // This is executed in case revert() was used // or there was a failing assertion, division // by zero, etc. inside getData. errorCount++; return (0, false); } } }