Rust swap

文章目录

rust 复制代码
    /*fn swap<'a>(a: &'a mut String, b:&'a mut String){
        let tmp = *a;
        *a = *b;
        *b = tmp;
    }
    
    let mut a = "aaa".to_string();
    let mut b = "bbb".to_string();
    
    swap(&mut a, &mut b);*/


    let mut a = 111;
    let mut b = 222;
    
    fn swap<'a>(a: &'a mut i32, b:&'a mut i32){
        let tmp = *a;
        *a = *b;
        *b = tmp;
    }


    /*
    fn swap(mut a:  i32, mut b:  i32){
        let tmp = a;
        a = b;
        b = tmp;
    }
    swap( a,  b);
    */
    
    swap(&mut a, &mut b);

最高效方式

rust 复制代码
    let mut a = "aaa".to_string();
    let mut b = "bbb".to_string();
    
    std::mem::swap(&mut a, &mut b);
相关推荐
крон6 分钟前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan39 分钟前
c++ 单例模式
开发语言·c++·单例模式
coderSong256841 分钟前
Java高级 |【实验八】springboot 使用Websocket
java·spring boot·后端·websocket
老胖闲聊1 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot
Blossom.1181 小时前
使用Python和Scikit-Learn实现机器学习模型调优
开发语言·人工智能·python·深度学习·目标检测·机器学习·scikit-learn
Mr_Air_Boy2 小时前
SpringBoot使用dynamic配置多数据源时使用@Transactional事务在非primary的数据源上遇到的问题
java·spring boot·后端
曹勖之2 小时前
基于ROS2,撰写python脚本,根据给定的舵-桨动力学模型实现动力学更新
开发语言·python·机器人·ros2
豆沙沙包?2 小时前
2025年- H77-Lc185--45.跳跃游戏II(贪心)--Java版
java·开发语言·游戏
军训猫猫头2 小时前
96.如何使用C#实现串口发送? C#例子
开发语言·c#
咖啡啡不加糖3 小时前
Redis大key产生、排查与优化实践
java·数据库·redis·后端·缓存