584 views|2 replies

6834

Posts

11

Resources
The OP
 

"Rust in Action" Learning Generic Functions [Copy link]

In order for a function to receive and process multiple possible input types, Rust introduces generic types.

When matching multiple types, the trait function is used to check whether the input function can support the operation.

The author gives the following function:

use std::ops::{Add};
use std::time::{Duration};

fn add<T: Add<Output = T>>(i: T, j: T) -> T {
    i + j
}

fn main() {
    let floats = add(1.2 , 3.4);
    let ints = add(10,20);
    let durations = add(
        Duration::new(5,0),
        Duration::new(10, 0)
        
    );
    println!("{}",floats);
    println!("{}", ints);
    println!("{:?}", durations);
}

Import the Add trait from std::ops into the current local scope at the beginning.

Imports the Duration type from std::time into the current local scope.

From the above examples, the function signature becomes very complicated. It takes some patience to understand these complex function signatures. The author gives the following principles:

1. Lowercase letters (such as i and j) represent variables.

2. A single uppercase letter (such as T) indicates a generic type.

3. Names that start with a capital letter (such as Add) are trait names or names of specific types, such as String and Duration.

4. Labels (such as 'a) represent life cycle parameters.

At this point, in fact, under the safety of Rust, programmers need to have very solid basic skills, otherwise reading the code will be very difficult.

Come on, study hard!

This post is from Programming Basics

Latest reply

It can receive and process a variety of possible input types, but what about output?   Details Published on 2024-4-19 23:30
 

6027

Posts

6

Resources
2
 

It can receive and process a variety of possible input types, but what about output?

This post is from Programming Basics

Comments

Output, it seems to point to T, but it does not do trait, it should be multiple, if the output value is used in other places, then do trait in other places. Here is direct printf. The moderator reminded me well, but I haven't learned it yet.  Details Published on 2024-4-20 07:31
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

6834

Posts

11

Resources
3
 
Qintianqintian0303 posted on 2024-4-19 23:30 It can receive and process a variety of possible input types, but what about the output?

Output, it seems to point to T, but it does not do trait, it should be multiple, if the output value is used in other places, then do trait in other places. Here is direct printf. The moderator reminded me well, but I haven't learned it yet.

This post is from Programming Basics
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Featured Posts
High-frequency power transformer design principles, requirements and procedures

Xu Zewei, editor of International Electronic Transformer    Abstract: Starting from the high-frequency power transfor ...

Assembly Language Programming [Download]

Chapter 1 Basic Knowledge 1.1 Assembly Language and Its Characteristics 1.2 Data Representation 1.3 8086 CPU and Registe ...

Infrared remote control receiving circuit (T9149A)

Infrared remote control receiving circuit (T9149A)

MSP430F6638 MCU FLL——Frequency Locked Loop

481229 Calculation formula: fDCOCLK ÷ = fFLLREFCLK ÷ n 【Note】: D: FLLD, default is 2 N: FLLN, default is 31 n: ...

Highly recommend a good introductory book on digital signal processing

I have been learning theoretical knowledge about digital signal processing recently. I used to read the textbooks I used ...

AC high voltage linear scheme to control two groups of lamps

Hello everyone! There is a case now, which requires a high-voltage linear solution, North America, AC120V lamps. There ...

IOT desk lamp software design

This post was last edited by skyworth74 on 2021-7-4 14:00 IOT desk lamp infrastructure To realize the IOT function, b ...

i.MX6ULL Embedded Linux Development 1-Preliminary Study on Uboot Transplantation

This post was last edited by DDZZ669 on 2021-7-27 23:05 This series of tutorials uses the ARM development board of the ...

Xintang M2354 Fault Injection Attack Protection

Abstract Shortly after Arm, a global mobile computing IP giant, launched its new isolation technology TrustZone, the hac ...

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list