---
title: "Parse YAML scalars as strings in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-scalars-as-strings/
description: "Read unquoted YAML booleans and numbers as strings in Rust with the Failsafe schema. Keep the mapping and sequence structure intact."
version: "1.2.0"
---

# Parse YAML scalars as strings in Rust

**oneyaml** is a free, open-source YAML library for Rust from Recursive AI. MIT or Apache-2.0, no unsafe code.

[crates.io](https://crates.io/crates/oneyaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust) · [Docs](https://recursiveai.org/oneyaml/rust/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-scalars-as-strings/read-yaml-scalars-as-strings.zip)

The YAML 1.2 Failsafe schema reads unquoted values like `true` and `007` as strings.

```shell
cargo add oneyaml
```

```rust
use oneyaml::{Schema, Settings, Value};
fn main() -> Result<(), oneyaml::Error> {
    let yaml = "flags: [true, 007]\n";
    let opts = Settings::new()
        .schema(Schema::Failsafe);
    let doc: Value =
        oneyaml::from_str_with(yaml, &opts)?;
    let flags = &doc["flags"];
    println!("{:?}", flags[0].as_str());
    println!("{:?}", flags[1].as_str());
    Ok(())
}
```

[The example](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-scalars-as-strings/read-yaml-scalars-as-strings.zip) is the full program below: unzip it and `cargo run`. To run only the code above, paste it into src/main.rs of a new `cargo new example` project and `cargo run`.

Both items print as strings:

```text
Some("true")
Some("007")
```

## Full program

The download uses Rust 1.85 or later and reads this `config.yaml`:

```yaml
flags: [true, 007]
```

```rust
use oneyaml::{Schema, Settings, Value};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let yaml = std::fs::read_to_string("config.yaml")?;
    let settings = Settings::new().schema(Schema::Failsafe);
    let doc: Value = oneyaml::from_str_with(&yaml, &settings)?;
    let flags = &doc["flags"];
    for index in 0..2 {
        println!("{:?}", flags[index].as_str());
    }
    Ok(())
}
```

`cargo run` prints:

```text
Some("true")
Some("007")
```

Malformed YAML returns a parse error; Failsafe changes how scalar types are chosen, not which YAML syntax is valid.

## Variations

**Check a null-looking value.** In the snippet at the top, replace the `yaml` line with this one. The same code prints `Some("null")` and `Some("~")`.

```rust
let yaml = "flags: [null, ~]\n";
```

## Related recipes

- [Read YAML without a Rust struct](https://recursiveai.org/oneyaml/rust/cookbook/read-yaml-without-a-schema/)
- [Read YAML 1.1 booleans in Rust](https://recursiveai.org/oneyaml/rust/cookbook/yaml-1-1-booleans/)

---

## The short answer

Pass `Settings::new().schema(Schema::Failsafe)` to `from_str_with` and read the YAML into a `Value`. In oneyaml-rust, Failsafe reads plain scalars (unquoted single values) as strings. The `flags` key still points to a list; only its two items change type compared with a default Core read. In particular, `007` stays `"007"` instead of becoming the number `7`.

## Do strings need quotes in YAML?

Not always. Under the [YAML 1.2 Core schema](https://yaml.org/spec/1.2.2/#103-core-schema), a plain word like `ready` is already a string, but `true`, `007`, and `null` resolve to a boolean, an integer, and a null. If you control the source and only a few fields are text, quote those values in the file and leave the reader on Core. Here the ID is text and the other items still have their normal types:

```yaml
flags: [true, "007", null]
```

If you don’t control the file and the whole input is meant to be text, use Failsafe for that read rather than rewriting the YAML. This also affects plain mapping keys, not just values: a key written as `007` is a string key. The [Failsafe schema](https://yaml.org/spec/1.2.2/#101-failsafe-schema) still recognizes mappings and sequences; it’s not the same as reading the entire file into one string.

## Does YAML support multiline strings?

Yes. A value can be a [block scalar](https://yaml.org/spec/1.2.2/#81-block-scalar-styles), where `|` preserves line breaks and `>` folds ordinary line breaks into spaces. Failsafe controls the scalar’s **type**, not how its text is decoded. If you need the exact source spelling, including quote marks, indentation, or comments, keep the source or use the document API instead of relying on `Value` to round-trip the file. Failsafe is useful for preserving `007` as a value; it doesn’t preserve the YAML presentation around it.
