# 深度理解之三：ctx.ctx.tcx.sess.source_map()

You want to print the source code of the expression given its `Span`. The `Span` object defines a range in the source code, so you can retrieve the source code that it covers. The following will show you how to print the source code of the expression in addition to the existing information:

> [https://doc.rust-lang.org/stable/nightly-rustc/rustc\_middle/dep\_graph/trait.DepContext.html#tymethod.sess](https://doc.rust-lang.org/stable/nightly-rustc/rustc_middle/dep_graph/trait.DepContext.html#tymethod.sess)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691647922376/bfa49bbe-80fb-4ebb-8aca-e7d41c7933d1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691648015198/409d77e7-23ff-4990-9e9e-4b63d01472cc.png align="center")

Returns the source snippet as `String` corresponding to the given `Span`.

```rust
let span = arg.span;
let lo = ctx.ctx.tcx.sess.source_map().lookup_char_pos(span.lo());
let hi = ctx.ctx.tcx.sess.source_map().lookup_char_pos(span.hi());
            
// Extract the source code given the span.
let source_code = ctx.ctx.tcx.sess.source_map().span_to_snippet(span).unwrap_or("<source code extraction failed>".to_string());
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1691647985590/6ce5a698-9cfa-4e5b-bfaf-731729645047.png align="center")
