mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-22 11:36:23 +00:00
49 lines
1.1 KiB
Markdown
49 lines
1.1 KiB
Markdown
---
|
|
date: 2020-11-20
|
|
id: e3a6f28a-27fd-489d-b2b4-de6d2c149a60
|
|
title: as~ptrrange~
|
|
---
|
|
|
|
# Description
|
|
|
|
as~ptrrange~[^1] returns the two raw pointers spanning the slice.
|
|
|
|
The returned range is half-open, which means that the end pointer points
|
|
one *past* the last element of the slice. This way, an empty slice is
|
|
represented by two equal pointers, and the difference between the two
|
|
pointers represents the size of the slice.
|
|
|
|
This function is useful for interacting with foreign interfaces which
|
|
use two pointers to refer to a range of elements in memory, as is common
|
|
in C++.
|
|
|
|
# Declaration
|
|
|
|
``` rust
|
|
pub fn as_ptr_range(&self) -> Range<*const T>
|
|
```
|
|
|
|
# Traits for Range\<A\>
|
|
|
|
``` rust
|
|
impl<A> Iterator for Range<A> where
|
|
A: Step, type Item = A;
|
|
```
|
|
|
|
# Syntax
|
|
|
|
``` rust
|
|
#![allow(unused)]
|
|
fn main() {
|
|
let a = [1, 2, 3];
|
|
let x = &a[1] as *const _;
|
|
let y = &5 as *const _;
|
|
|
|
assert!(a.as_ptr_range().contains(&x));
|
|
assert!(!a.as_ptr_range().contains(&y));
|
|
}
|
|
```
|
|
|
|
# Footnotes
|
|
|
|
[^1]: <https://doc.rust-lang.org/std/primitive.slice.html#method.as_ptr_range>
|