fn calculate_length(s: &String) -> usize {
s.len()
}
fn append_suffix(s: &mut String) {
s.push_str(" world");
fn take_ownership(s: String) {
println!("Took ownership: {}", s);
} // s is dropped here
fn main() {
let message = String::from("hello");
use anyhow::{Context, Result};
use std::fs;
fn load_config(path: &str) -> Result<String> {
fs::read_to_string(path)
.with_context(|| format!("failed to read config from {}", path))
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("file not found: {0}")]
NotFound(String),
use std::fs;
use std::num::ParseIntError;
fn read_port(path: &str) -> Result<u16, Box<dyn std::error::Error>> {
let contents = fs::read_to_string(path)?;
let port: u16 = contents.trim().parse()?;
class PostsController < ApplicationController
def create
@post = current_user.posts.build(post_params)
if @post.save
flash[:success] = 'Post was successfully created.'
import { useEffect, useState, useRef } from 'react'
interface UseInViewOptions {
threshold?: number | number[]
rootMargin?: string
triggerOnce?: boolean
module Posts
class CreateService
def initialize(author:, params:)
@author = author
@params = params
end
import { useReducer } from 'react'
interface FormState {
values: Record<string, any>
errors: Record<string, string>
touched: Record<string, boolean>
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :posts do
resources :comments, only: [:index, :create]
end
import { useForm } from 'react-hook-form'
import { useState } from 'react'
import api from '@/services/api'
interface SignupFormData {
email: string
class SlugValidator < ActiveModel::Validator
SLUG_REGEX = /\A[a-z0-9]+(?:-[a-z0-9]+)*\z/
def validate(record)
slug = record.send(options[:attribute] || :slug)