# Help understanding the \`ref t\` syntax

**URL:** <https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779>\
**Category:** help\
**Created:** [October 19, 2019, 6:51am UTC](https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779 "2019-10-19T06:51:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SRGOM](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/srgom/32/14923_2.png) [@SRGOM](https://users.rust-lang.org/u/SRGOM)\
**Post date:** [October 19, 2019, 6:51am UTC](https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779/1 "2019-10-19T06:51:50Z")

</div>

Hello, I need some help understanding the `ref t` syntax.

From: [std::option - Rust](https://doc.rust-lang.org/std/option/index.html#options-and-pointers-nullable-pointers)

```rust
fn check_optional(optional: Option<Box<i32>>) {
    match optional {
        Some(ref p) => println!("has value {}", p),

```

Here the statement is that try to pull `Box<i32>` as a `ref p`. In equation terms- `ref p` = `Box(9)`. `Deref` both sides `=>` `p = Box(9).deref()`. `p = 9`.

But from: [std::option - Rust](https://doc.rust-lang.org/std/option/index.html#examples)

```rust
msg = Some( "howdy" )
if let Some(ref m) = msg {

```

doesn't seem to be following that equation, `Some( "howdy" )` = `Some(ref m)` `=>` `m = "howdy".deref()` ?

How do I understand the `ref` syntax conceptually?

Is this deliberately inconsistent for language ergonomics?

Only to mention- Scala would have done it slightly differently which would have been clearer too-

1-

```rust
match optional{
  Some( Ref( p ) ) => ...

```

2-

```rust

if let Some(Deref(m)) = msg 

```

This is both mathematically consistent and IMHO easier to glance.

---

<div class="post-metadata">

**Author:** ![pcpthm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/pcpthm/32/11377_2.png) [@pcpthm](https://users.rust-lang.org/u/pcpthm)\
**Post date:** [October 19, 2019, 7:23am UTC](https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779/2 "2019-10-19T07:23:44Z")

</div>

`ref` is not related to `Deref`. The deref coercion is automatically inserted to some places but doesn't affect the matched variables directly.

If type `T` is matched to `ref x` then type of `x` is `&T`. But then why not just match for `x` and use `&x`? Consider this code:

```rust
#[derive(Debug)]
struct S;
fn use_s(_: &S) {}

let x: Option<S> = Some(S);
if let Some(ref y) = x {
    /* y : &S */
    use_s(y);
}
dbg!(x);

```

If I remove `ref`,

```rust
if let Some(y) = x {
    /* y : S */
    use_s(&y);
}
dbg!(x);

```

It doesn't compile. The error tells use `x` is moved by the pattern matching but later used.

It is similar to that "&some\_struct.field" doesn't move `some_struct` out.  
Technically, pattern matching matches against "place" instead of "value" (if you are familiar with C++, you can read "place" as "lvalue").

Today, the `ref` pattern is almost superseded by the ["match ergonomics"](https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md) which automatically insert `&` pattern and `ref` pattern to appropriate places.  
The `Option` example can be written as:

```rust
let msg: Option<&str> = Some("howdy");
if let Some(m) = &msg { /* m : &&str */ }

```

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [October 19, 2019, 4:58pm UTC](https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779/3 "2019-10-19T16:58:45Z")

</div>

> [@Ref keyword versus &](https://users.rust-lang.org/t/ref-keyword-versus/18818/5):
>
> ref on the left side of = is the same as & on the right. let ref x = 1; let x = &1; & on the left side of = is the same as \* on the right. let &y = x; let y = \*x;

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [January 17, 2020, 4:58pm UTC](https://users.rust-lang.org/t/help-understanding-the-ref-t-syntax/33779/4 "2020-01-17T16:58:50Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
