wiki/content/20201120095739-as_ptr_range.md

50 lines
1.1 KiB
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-30 17:04:36 +00:00
date: 20201120
2024-05-06 20:40:05 +00:00
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>