Tag:Linghu

  • Rust compiles conditionally in cargo

    Time:2021-3-4

    explain stayRust conditional compilationIn, we briefly introduced conditional compilation, but in the example, we did not show how to compile with cargo. Next, we will introduce how to compile with cargo. Source code //src/main.rs #[cfg(some_condition)] fn conditional_function() { println!(“condition met!”); } fn main() { conditional_function(); println!(“Hello, world!”); } Cargo.toml file stay cargo.toml Add the following […]

  • A simple example of using leveldb in rust programming

    Time:2021-2-14

    preface Recently, I’m going to write a perfect teaching demo of blockchain with rust, considering the use of leveldb in persistence. Through consulting the documents, we found that rust has provided an interface to use leveldb. The official example has been modified to run through. Examples Source code //src/main.rs use std::{env, fs}; use leveldb::database::Database; use […]

  • 006 rust asynchronous programming, stream introduction

    Time:2021-2-12

    Introduction to stream StreamandFutureSimilar, butFutureCorresponding to aitemChanges in the state of, andStreamIt is similar toiteratorTo get multiple values before the end. Or we can simply understand it as,StreamIt’s made up of a series ofFutureWe can start fromStreamRead eachFutureUntilStreamend. Stream trait definition The definition is as follows: trait Stream { type Item; fn poll_next(self: Pin<&mut Self>, […]

  • Introduction to rustyline

    Time:2021-2-4

    Recently, when writing the teaching demo of blockchain, I need to write a cli client to interact with users. Finally, I found rustyline. I typed it again according to the official example and found it very comfortable to use, so I recommend it to you. Examples configuration file modify Cargo.toml File: [dependencies] rustyline = “6.2.0” […]

  • 010 rust asynchronous programming, using the select macro conditions

    Time:2021-2-3

    Using the select macro Future used in select must implement unpin trait and future trait. The reason why unpin must be implemented is that the future used in select is not obtained by value, but by variable reference. By not acquiring the ownership of future, the unfinished future can continue to be used after calling […]

  • 008 rust asynchronous programming, select macro introduction

    Time:2021-2-2

    Select macro The select macro also allows concurrent execution of future, but it is different from join and try_ The difference is that the select macro will return as long as it has a future return. Examples Source code use futures::{select, future::FutureExt, pin_mut}; use tokio::runtime::Runtime; use std::io::Result; async fn function1() -> Result<()> { tokio::time::delay_for(tokio::time::Duration::from_secs(10)).await; println!(“function1 […]

  • 011 rust asynchronous programming, return error

    Time:2021-1-31

    What happens when a return error occurs in a rust asynchronous block? This section explains this knowledge point. Examples Source code use futures; async fn foo() { “foo” } fn main() { futures::executor::block_on(foo()); } configuration file [dependencies] futures = “0.3” This example reports an error as follows: error[E0308]: mismatched types –> src/main.rs:20:2 | 20 | […]

  • 012 rust asynchronous programming, used in async block?

    Time:2021-1-30

    Can we use question marks in rust asynchronous programming as in synchronous programming? Let’s have a try. Examples Source code [dependencies] futures = “0.3” configuration file use futures; async fn foo() -> Result<(), String>{ “foo”; Ok(()) } async fn func() -> Result<(), String>{ let fut = async { foo().await?; Ok::<(), String>(()) // <- note the […]

  • 013 rust asynchronous programming, send trait related

    Time:2021-1-29

    Whether async FN future is send depends on whether non send types are retained on the. Await point. The compiler does its best to estimate the save time of the value at the. Await point. Examples Source code use std::rc::Rc; #[derive(Default)] struct NotSend(Rc<()>); async fn bar() {} async fn foo() { NotSend::default(); bar().await; } fn […]

  • 014 rust asynchronous programming, recursive

    Time:2021-1-23

    Internally, async FN creates a state machine type that contains each waiting child future. So what happens when it comes to recursion? Examples Example 1 Source code use futures; async fn first() {} async fn second() {} async fn foo() { first().await; second().await; } fn main() { futures::executor::block_on(foo()); } to configure [dependencies] futures = “0.3” […]

  • 009 rust asynchronous programming, select macro using default and complete

    Time:2021-1-22

    explain In the previous section, we briefly introduced the select macro. In fact, in the select macro, you can also use default and complete. The former indicates that no branch has been completed, while the latter indicates that all branches have been completed and no further progress will be made. Examples source code use futures::{future, […]

  • Rust programming video tutorial (Advanced) — 014refcell and internal variability

    Time:2021-1-16

    Video address Headline address:https://www.ixigua.com/i677586170644791348…Station B address:https://www.bilibili.com/video/av81202308/ Source address GitHub address:https://github.com/anonymousGiga/learn_rus… Explanation content 1. Internal variability: allows data to be changed when using immutable references. 2. By checking borrowing rules at run time (usually at compile time) by refcell, refcell represents the unique ownership of its data.Similar to RC, refcell can only be used in single […]