RustDay06------Exercise[91-100]

91.将指针还原成指定类型

因为指针不知道里面具体有什么,所以一般约定打上unsafe

申明开发者自己对该部分可用性负责,且在调试的时候也能起强调作用

// tests6.rs
//
// In this example we take a shallow dive into the Rust standard library's
// unsafe functions. Fix all the question marks and todos to make the test
// pass.
//
// Execute `rustlings hint tests6` or use the `hint` watch subcommand for a
// hint.// I AM NOT DONEstruct Foo {a: u128,b: Option<String>,
}/// # Safety
///
/// The `ptr` must contain an owned box of `Foo`.
unsafe fn raw_pointer_to_box(ptr: *mut Foo) -> Box<Foo> {// SAFETY: The `ptr` contains an owned box of `Foo` by contract. We// simply reconstruct the box from that pointer.let mut ret: Box<Foo> = unsafe { Box::from_raw(ptr) };// todo!("The rest of the code goes here")ret.b = Some("hello".to_owned());ret
}#[cfg(test)]
mod tests {use super::*;use std::time::Instant;#[test]fn test_success() {let data = Box::new(Foo { a: 1, b: None });let ptr_1 = &data.a as *const u128 as usize;// SAFETY: We pass an owned box of `Foo`.let ret = unsafe { raw_pointer_to_box(Box::into_raw(data)) };let ptr_2 = &ret.a as *const u128 as usize;assert!(ptr_1 == ptr_2);assert!(ret.b == Some("hello".to_owned()));}
}

92.配置本地环境

首先我们得知道配置文件放在build.rs里面!!!

首先参考文档之后写上指令

"rustc-env=TEST_FOO={}",

然后注释掉下面的 your_command变量即可通过test7

93. 函数签名设置编译参数

这个标签指定了编译的配置参数key-value

设置编译的key-value即可,详情参照文档

let your_command = "rustc-cfg=feature = \"pass\"";

 

 94.设置#[link_name = "myName"]和#[no_mangle]获取原汁原味特定的函数名

有时候经过名称修饰或者编译,函数名会发生变化,在同语言下没有影响,但是在不同语言交互的时候可能会找不到指定的函数名,所以将函数名称固定可以避免此种情况发生

// tests9.rs
//
// Rust is highly capable of sharing FFI interfaces with C/C++ and other statically compiled
// languages, and it can even link within the code itself! It makes it through the extern
// block, just like the code below.
//
// The short string after the `extern` keyword indicates which ABI the externally imported
// function would follow. In this exercise, "Rust" is used, while other variants exists like
// "C" for standard C ABI, "stdcall" for the Windows ABI.
//
// The externally imported functions are declared in the extern blocks, with a semicolon to
// mark the end of signature instead of curly braces. Some attributes can be applied to those
// function declarations to modify the linking behavior, such as #[link_name = ".."] to
// modify the actual symbol names.
//
// If you want to export your symbol to the linking environment, the `extern` keyword can
// also be marked before a function definition with the same ABI string note. The default ABI
// for Rust functions is literally "Rust", so if you want to link against pure Rust functions,
// the whole extern term can be omitted.
//
// Rust mangles symbols by default, just like C++ does. To suppress this behavior and make
// those functions addressable by name, the attribute #[no_mangle] can be applied.
//
// In this exercise, your task is to make the testcase able to call the `my_demo_function` in
// module Foo. the `my_demo_function_alias` is an alias for `my_demo_function`, so the two
// line of code in the testcase should call the same function.
//
// You should NOT modify any existing code except for adding two lines of attributes.// I AM NOT DONEextern "Rust" {fn my_demo_function(a: u32) -> u32;#[link_name = "my_demo_function"]fn my_demo_function_alias(a: u32) -> u32;
}mod Foo {#[no_mangle]// No `extern` equals `extern "Rust"`.fn my_demo_function(a: u32) -> u32 {a}
}#[cfg(test)]
mod tests {use super::*;#[test]fn test_success() {// The externally imported functions are UNSAFE by default// because of untrusted source of other languages. You may// wrap them in safe Rust APIs to ease the burden of callers.//// SAFETY: We know those functions are aliases of a safe// Rust function.unsafe {my_demo_function(123);my_demo_function_alias(456);}}
}

ok完结

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/118663.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c语言从入门到实战——分支和循环

分支和循环 前言1. if语句1.1 if1.2 else1.3 分支中包含多条语句1.4 嵌套if1.5 悬空else问题 2. 关系操作符3. 条件操作符4. 逻辑操作符&#xff1a;&& , || , &#xff01;4.1 逻辑取反运算符4.2 与运算符4.3 或运算符4.4 练习&#xff1a;闰年的判断4.5 短路 5. swit…

鸡尾酒学习——长岛冰茶

长岛冰茶 1、材料&#xff1a;冰块&#xff08;或者雪莲&#xff09;、白朗姆、伏特加、龙舌兰、金酒、柠檬、君度或者白兰地、可乐&#xff1b; 2、口感&#xff1a;酸甜苦口味&#xff0c;酒的苦涩较为明显&#xff08;怀疑是自己放了过多的柠檬汁导致苦涩感明显&#xff09…

BeanUtils

BeanUtils BeanUtils类是Apache Commons BeanUtils库中的一个工具类&#xff0c;用于简化JavaBean的操作。它提供了一组方法&#xff0c;可以方便地对JavaBean进行属性的复制、获取和设置。 常用的BeanUtils方法包括&#xff1a; copyProperties(Object dest, Object orig)&a…

git:删除上一次 commit 中的某个文件,并重新提交

1. 首先&#xff0c;使用以下命令删除要移除的文件&#xff1a; git rm 文件路径将 “文件路径” 替换为要删除的文件的实际路径。这将从 Git 仓库中删除该文件&#xff0c;并将其放入暂存区。git rm 相当于 linux rm git add 命令。 2. 使用以下命令修改上一次的提交&#…

Sui提供dApp Kit 助力快速构建React Apps和dApps

近日&#xff0c;Mysten Labs推出了dApp Kit&#xff0c;这是一个全新的解决方案&#xff0c;可用于在Sui上开发React应用程序和去中心化应用程序&#xff08;dApps&#xff09;。mysten/dapp-kit是专门为React定制的全新SDK&#xff0c;旨在简化诸如连接钱包、签署交易和从RPC…

若依微服务上传图片文件代理配置

在使用若依微服务文件上传时候,文件上传成功会上传到D:/ruoyi/uploadPath目录下。默认使用9300端口进行访问图片文件,现在我想把它代理到80端口应该怎么做呢? 配置前:http://localhost:9300/statics/2023/09/24/test.jpg 配置后:http://localhost/statics/2023/09/24/test…

JWT的登录认证与自校验原理分析

目录 一、JWT的概述 1.什么是JWT&#xff1f; 2.JWT的用户认证 3.JWT解决了什么问题&#xff1f; 4.关于JWT中的签名如何理解&#xff1f; 5.JWT的优势 二、JWT的结构 1.令牌的组成&#xff1a; 2.JWT的工具类 3.JWT所需的依赖 4.JWT登录生成Token的原理 三、JWT的自…

浅谈智能制造

智能制造 如今&#xff0c;同一版本同一型号的手机&#xff0c;几乎是一模一样的。当我们说去选购商品&#xff0c;其实是在有限的型号中选择我们需要的那一款。可是&#xff0c;人的需求千变万化&#xff0c;为什么偏偏要归结到几个固定的型号上去呢&#xff1f;每个人不应该…

图片放大镜效果

安装&#xff1a; vueuse 插件 npm i vueuse/core 搜索&#xff1a; useMouseInElement 方法 <template><div ref"target"><h1>Hello world</h1></div> </template><script> import { ref } from vue import { useM…

【Python 算法】信号处理通过陷波滤波器准确去除工频干扰

对于一个信号来说通常汇入工频噪声往往是因为交流电产生的电泳&#xff0c;影响了我们信号采集导致信号上存在工频干扰。 那么matlab去除工频干扰可以通过陷波滤波器实现。 通常使用scipy.signal实现信号的处理。 Scipy的信号处理模块&#xff08;scipy.signal&#xff09;来创…

day02:DML DQL DCL

目录 一:DML 二:DCL 三:DCL 一:DML 1:概念:数据操作原因&#xff0c;对数据进行增删改。 2:三个操作 (1):增加:insert id name age gender 1 令狐冲 23 男 2(添加的数据)风清扬25男 1--->给指定字段添加数据:insert into 表名(字段1&#xff0c;字段2--)values…

攻防世界-web-FlatScience

1. 题目描述 打开链接&#xff0c;看到如下界面 界面上的链接都点击下&#xff0c;发现都是一些英文论文 这些暂时是我们从界面上能发现的全部信息了 2. 思路分析 && 解题过程 2.1 先将网站使用nikto命令扫描一下 我们发现除了显式的界面外&#xff0c;还有两个隐藏…

Mysql数据库 4.SQL语言 DQL数据操纵语言 查询

DQL数据查询语言 从数据表中提取满足特定条件的记录 1.单表查询 2.多表查询 查询基础语法 select 关键字后指定要查询到的记录的哪些列 语法&#xff1a;select 列名&#xff08;字段名&#xff09;/某几列/全部列 from 表名 [具体条件]&#xff1b; select colnumName…

Alexon:在云原生环境中快速部署应用服务

Alexon是一个旨在快速部署WEB应用服务到分布式系统中的工具&#xff0c;适用于云原生环境。 Alexon由SymeCloud Limited(syme.dev) 发布&#xff0c;使用GNU Guile编写而成&#xff0c;支持函数编程概念。 SymeCloud 公司主要致力于 AI-Infra 方面的研发&#xff0c;从 OpenAI …

华为昇腾NPU卡 大模型LLM ChatGLM2模型推理使用

参考&#xff1a;https://gitee.com/mindspore/mindformers/blob/dev/docs/model_cards/glm2.md#chatglm2-6b 1、安装环境&#xff1a; 昇腾NPU卡对应英伟达GPU卡&#xff0c;CANN对应CUDA底层&#xff1b; mindspore对应pytorch&#xff1b;mindformers对应transformers 本…

Vue-dvadmin-d2-crud-plus-自定义后台菜单-添加页面

文章目录 1.新建数据模型2.新建数据序列类3.新建数据视图4.配置路由5.前端新建View组件6.配置后台7.总结 django-vue-admin是一套全部开源的快速开发平台&#xff0c;毫无保留给个人及企业免费使用。 &#x1f9d1;‍&#x1f91d;‍&#x1f9d1;前端采用D2Admin 、Vue、Eleme…

Linux网络流量监控iftop

在 Linux 系统下即时监控服务器的网络带宽使用情况&#xff0c;有很多工具&#xff0c;比如 iptraf、nethogs 等等&#xff0c;但是推荐使用小巧但功能很强大的 iftop 工具【官网&#xff1a;http://www.ex-parrot.com/~pdw/iftop/】。iftop 是 Linux 系统一个免费的网卡实时流…

LinkedHashMap 简单实现LRU

要使用 LinkedHashMap 来实现LRU&#xff08;最近最少使用&#xff09;缓存&#xff0c;可以设置它的访问顺序为true&#xff0c;以便在每次访问一个元素时&#xff0c;将它移到最后&#xff0c;从而实现LRU的特性。以下是一个简单的Java示例&#xff1a; import java.util.Li…

Linux 内核定时器(高级字符设备五)

一、Linux 内核定时器介绍 在 Linux 内核中很多函数是基于定时器进行驱动的&#xff0c;但是内核定时器的精度并不高&#xff0c;所以不能作为高精度定时器使用。并且内核定时器的运行没有周期性&#xff0c;到达计时终点后会自动关闭。如果要实现周期性定时&#xff0c;就要在…

Path Gain and Channel Capacity for HAP-to-HAP Communications

文章目录 摘要实验仿真场景一&#xff1a; 距离变化对同海拔高度HAP的影响场景二&#xff1a;距离变化对不同海拔高度HAP通信的影响。场景三&#xff1a;平台高度和频率对HAP通信的影响四 信道容量 摘要 在这项研究中&#xff0c;我们重点分析了HAP之间的信道模型&#xff0c;…